Linq Evaluate Optional Types in Select Clause

I am getting a weird exception: by "Unknown Expression type: IIF(e.Accredited.Value, 1, 0)"

executing the following statement:

var x = from e in _EntityManager.TrainingCourses
            select new { Disabled = (e.Accredited.Value ? 1 : 0) };

      

Please, help!! How to evaluate (bool?) In select

thanks

+1


a source to share


2 answers


Does this code answer your question? Obviously checking the bool value clearly does the trick:

void Main()
{

    var a = new List<acc>() { 
        new acc(){Accredited = false}, 
        new acc(){Accredited = true}, 
        new acc(){Accredited = null}
        };

    var x = from e in a
        select new { Disabled = (e.Accredited == true ? 1 : 0) };

    foreach (var i in x)
    {
        Console.WriteLine(i);
    }
}
public struct acc
    {
       public bool? Accredited;
    }

      



Output: 0 1 0

+1


a source


Assuming which e.Accredited

is Nullable<bool>

( bool?

), try this:



var x = from e in _EntityManager.TrainingCourses
        select new { Disabled = (e.Accredited.HasValue && e.Accredited.Value) };

      

0


a source







All Articles