VB.NET Linq使用查询语法

时间:2015-08-06 18:53:36

标签: vb.net linq left-join linq-to-objects anonymous-types

我有一份申请表。每个应用程序可能有也可能没有与之关联的支持记录。

我可以加入的唯一方法是获取应用标题,看看它是否等于支持(产品)的LookupValue。但要获得该属性,我必须转换为FieldLookupValue。如果没有关联的支持记录,那么Linq查询中的.MoveNext()上会抛出空引用错误。以下是目前适用于具有相关支持的应用程序的内容,但是当它们不支持时会抛出错误。

Dim q =
    From a In apps.AsEnumerable()
    Group Join s In support.AsEnumerable()
    On a("Title").ToString() Equals CType(s("Product"), FieldLookupValue).LookupValue 
    Into Group
    From ajs In Group.DefaultIfEmpty()
    Select New With {
        .Name = a("Title"),
        .SupportEnd = IIf(ajs Is Nothing, Nothing, ajs("End"))
    }

我可以用任何方式比较On Statement中的匿名类型吗?我似乎无法在语法上获得正确的语法,或者可能不可能。我觉得可以解决空引用错误。我的失败尝试返回等于无法将类型与类型

的值进行比较
Dim q =
   From a In apps.AsEnumerable()
   Group Join s In support.AsEnumerable()
   On
   (New With {Key .AppName = a("Title").ToString()}) Equals
   (New With {Key .AppName = IIf(s Is Nothing, "nada", CType(s("Product"), FieldLookupValue).LookupValue)})
   Into Group
   From ajs In Group.DefaultIfEmpty()
   Select New With {
      .Name = a("Title"),
      .SupportEnd = IIf(ajs("End") Is Nothing, Nothing, ajs("End"))
   }

关于如何使用上述两种失败方法之一使这种连接工作的任何想法都会很棒。

1 个答案:

答案 0 :(得分:0)

一旦我根据支持列表中的随机选择创建了一个虚拟默认对象(我选择了索引0),我就用它进行比较,如图所示。然后使用Let语句在选择时可以轻松访问布尔值。

Dim q =
    From a In apps.AsEnumerable()
    Group Join s In support.AsEnumerable()
    On a("Title").ToString() Equals CType(s("Product"), FieldLookupValue).LookupValue 
    Into Group
    From ajs In Group.DefaultIfEmpty(support(0))
    Let NoSupport As Boolean = IIf(ajs.Equals(support(0)), True, False)
    Select New With {
        .Name = a("Title"),
        .SupportEnd = IIf(NoSupport, "No Support", ajs("End"))
    }
相关问题