c# - 在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.Nullable
1[System.Int32]]”的对象转换为类型“BerkshireHathaway.Models.report”。
感谢你的协助。我花了几个小时试图找到答案 - 如果这是一个常见问题,我深表歉意。
解决方案
您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
中的类和属性名称不同,我们使用 NOTcamelCase
或snake_case
.
仅供参考。
推荐阅读
- javascript - 如何编写将内容脚本应用于单页应用程序的 chrome 扩展程序?
- typescript - 如何在Javascript中访问对象中的嵌套字段?
- erlang - erlang:system_time/1 可以返回负数吗?
- angular - 路由更改时的角度@input值
- arrays - 将数组中的两个字节值组合并放入一个并打印它
- c++ - 具有返回值的模板推导类型的重载右值和左值引用及其实现
- arduino - 使用 Arduino 从 I2C LCD 输出错误的字符
- python - Django 数据操作效率
- javascript - 如何将向下倾斜的粒度合并到我的机器学习 tensorflow.js 算法中?
- python-3.x - 另一列的非NaN分组计数和pandas中相同列的具体计算