Linq在select子句中评估Nullable类型

时间:2009-05-14 14:09:43

标签: c# linq-to-entities

我得到一个奇怪的例外:"Unknown Expression type: IIF(e.Accredited.Value, 1, 0)" 运行以下语句:

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

请帮忙!!如何评估选择

中的(bool?)

由于

2 个答案:

答案 0 :(得分:1)

此代码是否回答了您的问题?明确地检查bool值似乎可以解决问题:

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;
    }

输出: 0 1 0

答案 1 :(得分:0)

假设e.AccreditedNullable<bool>bool?),请尝试以下操作:

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