我正在尝试掌握 Entity Framework,但有一件事真的让我很头疼。我仍然不完全确定术语并没有帮助,而且我试图避免同时学习 LINQ,所以谷歌搜索很困难。
我有两张表,公司和地址具有一对多的关系。如果我写以下内容:
ObjectQuery<Company> companies = queryContext.Companies.Include("Addresses");
看起来我得到了我想要的(公司 -> 结果视图 [0].Addresses.Count 是 > 0)
我现在想做的是将公司名称和所有地址绑定到 ASP.NET 应用程序中的网格视图
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>
这本身会引发错误(A field or property with the name 'Address' was not found on the selected data source
) - 我认为是因为公司 -> Results View[0].Name 存在,但 .Address 不存在(因为它隐藏在 Addresses 关系中)。绑定到 Addresses.Address 也无济于事。
我在这个线程的底部发现了一个非常丑陋的解决方法,但如果可能的话,我宁愿避免它。
有什么方法可以“展平”我的结果,以便顶级对象为所有包含的字段提供地址?
非常感谢任何帮助!