我有一个包含3个DbSet和一对多关系的上下文类: (1)地点 - 大学(很多) (1)大学 - 评级(很多)
我需要在wpf的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
 Rank" Width="60" DisplayMemberBinding="{Binding NationalRank }"/>
<GridViewColumn Header=" Quality of 
 Education" Width="70" DisplayMemberBinding="{Binding QualityOfEducation}"/>
<GridViewColumn Header=" Alumni of 
Employment" Width="70" DisplayMemberBinding="{Binding AlumniEmployment}"/>
<GridViewColumn Header=" Quality of 
 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 
Impact" Width="50" DisplayMemberBinding="{Binding BroadImpact}"/>
<GridViewColumn Header="Patents" Width="60" DisplayMemberBinding="{Binding Patents }"/>
<GridViewColumn Header="Score" Width="60" DisplayMemberBinding="{Binding Score }"/>
答案 0 :(得分:1)
您只选择了评级表中的字段,那么您希望如何显示大学和地点中的字段!
选项1 :
使用导航对象绑定大学和位置列,例如University.UniversityName
和University.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();
}