Search code examples
c#filterimplicit

Implicitly convert type 'string' to 'bool'


I have to deal to question: Filter all the Student in the list with Result is "Pass". But when I write a code like in the picture below, it always return wrong with caution like this: Cannot implicitly convert type 'string' to 'bool'. Although I used Convert.ToBoolean(t.Result) but it can't work?

Help me!

enter image description here

enter image description here


Solution

  • To Filter all the Student in the list with Result is "Pass" you need to add the condition as:

    lst.Where(t => t.Result=="Pass").ToList();
    

    You can do the same with a Boolean property instead for this string, so the property definition will be like:

    public bool  Result
    {
        get { return Score > 25; }           
    }
    

    So the iterative condition for Filtering all the Student in the list with Result is "Pass"(Score > 25) will be

    lst.Where(t => t.Result).ToList();