views:

345

answers:

2

Hi,

I get a weird exception: "Unknown Expression type: IIF(e.Accredited.Value, 1, 0)" running the following statement:

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

Please help!! How do I evaluate a (bool?) in the select

Thanks

A: 

Assuming e.Accredited is a Nullable<bool> (bool?), try this:

var x = from e in _EntityManager.TrainingCourses
        select new { Disabled = (e.Accredited.HasValue && e.Accredited.Value) };
Joel Coehoorn
this does work, but Disabled must be set to 1 or 0 not true or false. what it I have to return "Is disabled" or "is not disabled". I want to know how to use the ? : statement in a linq select clause
+1  A: 

Does this code answer your question? Checking for the bool value explicitly seems to do 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

Josh E