首页 > 解决方案 > 在Repeater中使用LINQ Join的结果

问题描述

我有一个用 LINQ 编写的连接查询,它连接两个表。

但是,我正在努力从两个表中访问属性,因为我使用主表作为转发器的“ItemType”声明,并且它没有显示新加入的列以供选择。

我觉得这是因为我需要从结果中创建一个对象/类,但我终生无法找到能够将结果从变量列表中放入转发器可以访问所有对象的对象中的要求结果列和记录。

代码隐藏文件

protected void Page_Load(object sender, EventArgs e)
{
    // Grab the CompanyID from the URL to parse into the query for company details page.
    int.TryParse(Request.QueryString["CompanyID"], out int companyIDURL);

    // Create queries that grab the company details from the company directory. 
    using (BerkshireHathaway.Models.BerkshireHathawayEntitiesNew wde = 
        new BerkshireHathaway.Models.BerkshireHathawayEntitiesNew())
    {
        // Should look to bring in the company reports too.

        var reports = (from reportTable in wde.reports
                       join reportCategory in wde.report_category
                       on reportTable.report_categoryID 
                       equals reportCategory.report_categoryID
                       where reportTable.companyID == companyIDURL

                       // This needs to be directly typed. I've been putting 
                       // into the array.
                       select new 
                       { 
                           reportsID = reportTable.reportID, 
                           reportTypeDescription = reportCategory.description, 
                           sortOrder = reportCategory.sort_order 
                       }).ToList();

        ReportGrid.DataSource = reports;
        ReportGrid.DataBind();
    }
}

.ASPX

<section id="Reports">
    <table>
        <asp:Repeater ID="ReportGrid" runat="server" 
                      ItemType="BerkshireHathaway.Models.report">
            <ItemTemplate>
                <tr>
                    <td>
                        <%#Item.report_category %>
                    </td>
                    <td>
                        <%# Item.report_title %>
                    </td>
                    <td>
                        <%# Item.report_date %>
                    </td>
                    <td>
                        <%# Item.summary %></td>
                    <td>
                        <a href="<%#Item.file_location %>" target="_blank">
                            Read Report.
                        </a>
                    </td>
                </tr>
            </ItemTemplate>
        </asp:Repeater>
    </table>
</selection>

对象调试(看起来正确,除了“匿名”类型

在此处输入图像描述

出现错误消息

关于匿名类型的错误消息

无法将类型为“<>f__AnonymousType3 3[System.Int32,System.String,System.Nullable1[System.Int32]]”的对象转换为类型“BerkshireHathaway.Models.report”。

感谢你的协助。我花了几个小时试图找到答案 - 如果这是一个常见问题,我深表歉意。

标签: c#asp.net.netlinq

解决方案


ItemType将中继器指定为,BerkshireHathaway.Models.report但是当您执行选择时,您没有指定对象的类型,这就是它变为匿名的原因。

相反,您可以这样做:

select new report { reportsID = reportTable.reportID, reportTypeDescription = reportCategory.description, sortOrder = reportCategory.sort_order})

“报告”new report应该在哪里BerkshireHathaway.Models.report。换句话说,与指定为ItemType中继器的类型相同。

边注

顺便说一句,您的命名方式与 C#PascalCase中的类和属性名称不同,我们使用 NOTcamelCasesnake_case.

仅供参考。


推荐阅读