如何检查可空布尔值是否为真?

时间:2015-03-20 13:31:05

标签: c# linq entity-framework

现在,我有这个有效,但它丑陋而长:

        var details = dc.SunriseShipment
            .Where(it => (it.isDeleted == null || it.isDeleted == false));

有更好的方法吗?我试过“it.isDeleted!= true”和“it.isDeleted ?? false == false”但是它们没有用。

3 个答案:

答案 0 :(得分:5)

试试这个:

.Where(it => !(it.isDeleted ?? false));

答案 1 :(得分:0)

.Where(it => it.isDeleted == (bool?)false);

答案 2 :(得分:0)

有一个GetValueOrDefault方法,当值为null时返回默认值:

var details = dc.SunriseShipment
.Where(it => !it.isDeleted.GetValueOrDefault(false));