I'm trying to get to grips with Entity Framework and there is one thing that's really tripping me up. It doesn't help that I'm still not totally sure of the terminology, and I'm trying to avoid learning LINQ at the same time, so googling is difficult.
I have two tables, company and addresses with a 1-to-many relationship. If I write the following:
ObjectQuery<Company> companies = queryContext.Companies.Include("Addresses");
It looks like I am getting what I want (companies -> Results View[0].Addresses.Count is > 0)
What I'd like to do now is bind the company names and all addresses to a gridview in an ASP.NET application
this.CompaniesGrid.DataSource = companies;
this.CompaniesGrid.DataBind();
<asp:GridView runat="server" ID="CompaniesGrid" AllowSorting="true">
<Columns>
<asp:BoundField DataField="Name" />
<asp:BoundField DataField="Address" />
</Columns>
</asp:GridView>
This on its own throws an error (A field or property with the name 'Address' was not found on the selected data source
) - I think because companies -> Results View[0].Name exists but .Address doesn't (because it's buried in the Addresses relationship). Binding to Addresses.Address doesn't help either.
I found one really ugly workaround at the bottom of this thread but I would rather avoid it if possible.
Is there any way to 'flatten' my results so that the top level objects provide addess to all included fields?
Any help much appreciated!