views:

257

answers:

1

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();
        }
+1  A: 

Hey,

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:

<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/

Brian