2

I am working with the Entity Framework model. With the help of LINQ, I am trying to load entities and their associated data in a single query (i.e. to implement eager loading).

Here's the code-behind:

protected void btn5_Click(object sender, EventArgs e)
{
    using (EStoreEntities ctx5 = new EStoreEntities())
    {
        var query = (from o in ctx5.Order_Details.Include("Order") select o);
                                                           //Order -navigation property

        tb5.Text = (queryas ObjectQuery).ToTraceString();
        gv5.DataSource = query;
        gv5.DataBind();
    }
}

I'm using the following ASP code:

<asp:Button ID="btn5" runat="server" Text="Button" onclick="btn5_Click" />
<asp:GridView ID="gv5" runat="server"></asp:GridView>
<asp:TextBox ID="tb5" runat="server" TextMode="MultiLine"></asp:TextBox>

How do I fix my code to make the Order table content display in my GridView control? Thank you in advance.

Glenn Slaven
  • 33,720
  • 26
  • 113
  • 165
Michael
  • 13,950
  • 57
  • 145
  • 288
  • I believe you must specify the properties of the related entities (`Order`) explicitely in the `asp:GridView` with dotted paths like `Order.ShippingDate`, etc. The automatic column binding only works for direct properties of the bound entity (`Order_Details`). Perhaps the example with the `asp:TemplateField` here helps: http://stackoverflow.com/a/2289408/270591. I think the problem is in your GridView definition, not in your EF query. – Slauma Dec 06 '11 at 19:13

1 Answers1

0
protected void btn5_Click(object sender, EventArgs e)
{ 
    using (EStoreEntities ctx5 = new EStoreEntities())
    {
        var query = ctx5.Order_Details.Select(x => new { 
                                                      Col1 = x.AAA,
                                                      Col2 = x.BBB,
                                                      Col3 = x.Order.AAA
                                                   });

        tb5.Text = (query as ObjectQuery).ToTraceString();
        gv5.DataSource = query;
        gv5.DataBind();
    }
}
Ivo
  • 8,172
  • 5
  • 27
  • 42