【问题标题】:LinQ: join on nullable property with value nullLinQ:加入值为null的可空属性
【发布时间】:2014-10-16 11:27:45
【问题描述】:

我有一个对象列表,其属性 customerId 是一个可为空的 System.Guid 属性。 我还有一个 System.Guid 类型的 id 列表,我还在这个列表中添加了一个 Guid.Empty 值。

我尝试对两者进行连接,但没有返回具有空 guid 的对象。

Dim dos = (From d In documents Join c In allowedCustomers On c Equals If(d.CustomerGuid = Nothing, System.Guid.Empty, d.CustomerGuid) Select d).Skip(10 * (pageNr - 1)).Take(10).ToList

怎么了?还有其他更好的方法吗?

【问题讨论】:

    标签: vb.net linq join


    【解决方案1】:

    您正在使用d.CustomerGuid = Nothing,但您必须使用d.CustomerGuid Is Nothing

    试试这个使用VB.NET's null-coalescing operator的方法。

    Dim query = From doc In documents
                Join custID In allowedCustomers
                On If(doc.CustomerGuid, Guid.Empty) Equals custID
                Skip 10 * (pageNr - 1)
                Take 10
                Select doc
    Dim docList = query.ToList()
    

    请注意,您可以通过多行来增加可读性,而且VB.NET的查询语法比C#强大,因此您可以在查询中使用SkipTake

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-11-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-09-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多