Linq Left加入VB.NET到匿名类型列表

时间:2015-06-22 13:09:39

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

我正在尝试使用Linq在Anonymous Typed对象列表上完成LEFT OUTER JOIN,其中数据是Microsoft.SharePoint.Client.ListItem 我想显示所有应用。任何拥有与之关联的支持记录的应用,我都会显示这些属性。任何没有相关支持记录的应用,我都会将这些字段显示为空。

更新 - 以下代码适用于已返回的第一个应用程序,该应用程序具有相关支持,但会在列表中没有相关支持的下一个应用程序上引发错误。我很难过为什么在枚举的.MoveNext()期间遇到空引用错误的原因。任何帮助将非常感激。

LINQ + DataBinding

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, "Unsupported", ajs("End"))
  }

gvApps.DataSource = q.AsQueryable()
gvApps.DataBind()

显示

<asp:GridView ID="gvApps" runat="server" CssClass="GvApps"
         AutoGenerateColumns="false" EnableViewState="true"
         GridLines="None" CellSpacing="-1">
      <Columns>
        <asp:TemplateField HeaderText="Application">
          <ItemTemplate>
            <%# DataBinder.Eval(Container.DataItem, "Name")%>
          </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Support End">
          <ItemTemplate>
            <%# DataBinder.Eval(Container.DataItem, "SupportEnd")%>
          </ItemTemplate>
        </asp:TemplateField>
      </Columns>
</asp:GridView>

错误

对象引用未设置为对象的实例。 Select New With {

堆栈跟踪

  • SPDataAccess._Lambda $ __ 5(VB $ AnonymousType_0`2 $ VB $ It1,ListItem appWithSupport)
  • System.Linq.d__31`3.MoveNext()
  • System.Web.UI.WebControls.GridView.CreateChildControls(IEnumerable dataSource,Boolean dataBinding)

1 个答案:

答案 0 :(得分:0)

            Dim AppsList =
            From a In applications.AsEnumerable()
            Group Join s In support.AsEnumerable()
            On Integer.Parse(a("ID")) Equals CType(s("Product"), FieldLookupValue).LookupId Into Group
            From appSupport In Group.DefaultIfEmpty()
            Order By a("Title") Ascending
            Select New With {
                .AppID = a("ID"),
                .AppViewUrl = AppSettings("ViewURL") & "&ListId={" & AppSettings("AppListGuid") & "}&ID=" & a("ID"),
                .AppName = a("Title"),
                .AppContact = ParseMultivaluedUser(a("App_x0020_Contact"), "No Contacts Listed"),
                .SupportEnd = If(appSupport IsNot Nothing AndAlso Date.TryParse(appSupport.Item("End"), New Date), Date.Parse(appSupport.Item("End")).ToShortDateString, Nothing),
                .Licenses = If(appSupport Is Nothing, Nothing, appSupport.Item("Quantity")),
                .StatusIcon = If(appSupport IsNot Nothing, GetStatusIcon(appSupport.Item("End")), "<span class=""StatusIco Tip NoSupport"" title=""No Support Record""> </span>"),
                .SupportTitle = If(appSupport Is Nothing, Nothing, appSupport("Title")),
                .AkaNames = a("AKA_x0020_Names"),
                .AkaOmit = a("AKA_x0020_Omit"),
                .Installs = GetInstalls(a("ID"), a("AKA_x0020_Names"), a("AKA_x0020_Omit"))
            }