简体   繁体   English

使用2个实体框架查询中的数据加载嵌套的转发器

[英]Loading a nested repeater with Data from 2 Entity Framework Queries

I have 2 tables A and B that have a many to many relationship. 我有2个具有多对多关系的表A和B。 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. 我的问题是,如何编写一个ObjectQuery或IQueryable查询以返回A中的父行和B中的子行,以便可以将它们用作内部和外部转发器的数据源。 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: 到目前为止,这是我的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();
        }

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: 如果类A具有B实体的集合,则可以将其直接绑定到DataSource属性,如下所示:

<asp:Repeater ... DataSource="<% Eval("Bs") %>">

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/ 检查一下: http : //thedatafarm.com/blog/data-access/inserting-many-to-many-relationships-in-ef-with-or-without-a-join-entity/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM