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

时间:2016-11-23 23:48:26

标签: wpf entity-framework xaml data-binding wpf-controls

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

我需要在wpf的listview中向所有评分显示查询结果。 但listView不显示大学和位置的字段。

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

1 个答案:

答案 0 :(得分:1)

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

选项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();
        }