【问题标题】:Linq to Entities : how to handle null values in database with a containsLinq to Entities:如何使用包含处理数据库中的空值
【发布时间】:2011-09-20 18:49:15
【问题描述】:

我正在使用 EF4。

我想用 linq to entity 写下面的语句

select * from address where address.satausId in (1,2,3,4)

数据库中的状态 ID 可能为空

我试过了

var statusesToFind = new List<int> {1, 2, 3, 4};

var AddressList = from sa in db.Address
                  where statusesToFind.Contains(sa.statusId)
                  select sa;

这给了我一个语法错误

奇怪的是,如果我尝试上述语句但使用作为关键字段的 sa.Id,它可以正常工作,我认为问题是由于数据库中的 statusId 允许空值.

如果有人能告诉我如何正确编写上述内容,我将不胜感激。

谢谢

【问题讨论】:

    标签: linq linq-to-entities


    【解决方案1】:

    make statustofind 一个可以为空的整数列表

    var statusesToFind = new List<int?> {1, 2, 3, 4};
    
    var AddressList = from sa in db.Address
                      where statusesToFind.Contains(sa.statusId)
                      select sa;
    

    【讨论】:

    • 您的解决方案有效,感谢您抽出宝贵时间回答我的问题,非常感谢。
    • 谢谢,这节省了一天的时间。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-03-22
    • 2010-09-27
    • 1970-01-01
    • 2011-01-11
    • 2010-12-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多