使用来自 2 个实体框架查询的数据加载嵌套转发器
我有 2 个表 A 和 B,它们具有多对多关系。我正在使用嵌套中继器来显示网页中的数据。我的问题是,如何编写一个 ObjectQuery 或 IQueryable 查询来返回 A 中的父行和 B 中的子行,以便我可以将它们用作内部和外部中继器的数据源。我有下面到目前为止编写的代码,但我不确定它是否正确或什至接近。
<asp:Repeater ID="A" runat="server"><br/>
<ItemTemplate><br/>
<h2 class="Information"><br/>
<%# Eval("Name") %> (<%#Eval("Abbreviation")%>)<br/>
</h2><br/>
<hr/><br/>
<p> <%# Eval("Description")%> </p><br/>
<asp:Repeater ID="B" runat="server"><br/>
<ItemTemplate><br/>
<li><br/>
<a href="..Pages/Category.aspx?<%# Eval("ID") %>"><br/>
<%# Eval("Name") %><br/>
</a><br/>
</li> <br/>
</ItemTemplate><br/>
</asp:Repeater><br/>
</ItemTemplate><br/>
</asp:Repeater>
这是到目前为止我的 C# 代码隐藏:
using (DBEntities connection = new DBEntities())
{
ObjectQuery<A> As = connection.A;
IQueryable<A> aQuery = from a in As
orderby a.SortOrder
select a;
TechnologyRepeater.DataSource = As;
DataBind();
}
I have 2 tables A and B that have a many to many relationship. I am using nested repeaters to show the data in a web page. My issue is, how do I write an ObjectQuery or an IQueryable query that returns the parent rows in A and the child rows in B so that I can use them as my data sources for my inner and out repeater. I have the code I wrote so far below but I am not sure if it is correct or even close.
<asp:Repeater ID="A" runat="server"><br/>
<ItemTemplate><br/>
<h2 class="Information"><br/>
<%# Eval("Name") %> (<%#Eval("Abbreviation")%>)<br/>
</h2><br/>
<hr/><br/>
<p> <%# Eval("Description")%> </p><br/>
<asp:Repeater ID="B" runat="server"><br/>
<ItemTemplate><br/>
<li><br/>
<a href="..Pages/Category.aspx?<%# Eval("ID") %>"><br/>
<%# Eval("Name") %><br/>
</a><br/>
</li> <br/>
</ItemTemplate><br/>
</asp:Repeater><br/>
</ItemTemplate><br/>
</asp:Repeater>
This is my C# codebehind so far:
using (DBEntities connection = new DBEntities())
{
ObjectQuery<A> As = connection.A;
IQueryable<A> aQuery = from a in As
orderby a.SortOrder
select a;
TechnologyRepeater.DataSource = As;
DataBind();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
多对多根据结构的不同设置不同的方式。如果类 A 具有 B 实体的集合,您可以将其直接绑定到 DataSource 属性,如下所示:
因此,这取决于对象模型中实体的引用方式,这又取决于多对多设置。看看这个:http://thedatafarm.com/blog/data-access/inserting-many-to-many-relationships-in-ef-with-or-without-a-join-entity/
Many to many is setup different ways depending on structure. If class A has a collection of B entities, you can bind it directly to the DataSource property as in:
So it depends on how the entities are referenced in your object model, which again varies depending on the many to many setup. Check this out: http://thedatafarm.com/blog/data-access/inserting-many-to-many-relationships-in-ef-with-or-without-a-join-entity/