如何在具有一对多关系的ListView实体中显示

How to display in ListView entities with one-to-many relationship

我有一个包含 3 个 DbSet 和一对多关系的上下文 class: (1)地点——大学(多处) (1)大学 - 评级(多)

而且我需要在 wpf 的列表视图中显示对所有评级的查询结果。 但是 listView 不显示来自大学和位置的字段。

查询示例:

  using (var c = new Context())
        {
            ratingListView.ItemsSource = (from w in c.Ratings
                     where w.Year.Value == 2012
                     select w).ToList();
        }

ListView 绑定:

<GridViewColumn Header="Rank" Width="Auto" DisplayMemberBinding="{Binding WorldRank}"/>
                    <GridViewColumn Header="Institution" Width="120" DisplayMemberBinding="{Binding Unviversity}"/>
                    <GridViewColumn Header="Location" Width="60" DisplayMemberBinding="{Binding Location }"/>
                    <GridViewColumn Header="National&#x0a;   Rank" Width="60" DisplayMemberBinding="{Binding NationalRank }"/>
                    <GridViewColumn Header=" Quality of &#x0a; Education" Width="70" DisplayMemberBinding="{Binding QualityOfEducation}"/>
                    <GridViewColumn Header="  Alumni of &#x0a;Employment" Width="70" DisplayMemberBinding="{Binding AlumniEmployment}"/>
                    <GridViewColumn Header=" Quality of &#x0a;    Faculty" Width="70" DisplayMemberBinding="{Binding QualityOfFaculty}"/>
                    <GridViewColumn Header="Publications" Width="70" DisplayMemberBinding="{Binding Publications }"/>
                    <GridViewColumn Header="Citations" Width="60" DisplayMemberBinding="{Binding Citations }"/>
                    <GridViewColumn Header=" Broad &#x0a;Impact" Width="50" DisplayMemberBinding="{Binding BroadImpact}"/>
                    <GridViewColumn Header="Patents" Width="60" DisplayMemberBinding="{Binding Patents }"/>
                    <GridViewColumn Header="Score" Width="60" DisplayMemberBinding="{Binding Score }"/>

您只选择了评级中的字段table,那么您希望如何显示大学和地点中的字段!

选项 1:

使用导航对象绑定大学和位置列,例如 University.UniversityNameUniversity.Location.LocationName(我编造了成员名称,因为您没有提供 EF 的整个结构)。

选项 2:

linq 查询中包含大学和位置。

 using (var c = new Context())
        {
            ratingListView.ItemsSource = (from w in c.Ratings
                                         join u in c.Universities
                                         on w.UniversityID equals u.ID
                                         join loc in c.Locations
                                         on u.LocationID equals loc.ID
                     where w.Year.Value == 2012
                     select new {loc.Location, u.University, w.field1, w.field2}).ToList();
        }