C# 如何从EntityCollection中筛选特定信息?
我想在C# 如何从EntityCollection中筛选特定信息?,c#,.net,linq,entity-framework-4,C#,.net,Linq,Entity Framework 4,我想在results变量中显示每个学生的成绩信息 我的数据库的设置方式是: Grade --- GradeStudent --- Student 因此,每一年,我都有一个独特的GradeStudent记录,记录着年级ID、学生ID以及记录的年份 我使用EntityFramework4作为我的ORM,我想在dataGridView中为每个学生显示年级名称 private void btnSearch_Click(object sender, EventArgs e) { StudentR
results
变量中显示每个学生的成绩信息
我的数据库的设置方式是:
Grade --- GradeStudent --- Student
因此,每一年,我都有一个独特的GradeStudent
记录,记录着年级ID、学生ID以及记录的年份
我使用EntityFramework4作为我的ORM,我想在dataGridView中为每个学生显示年级名称
private void btnSearch_Click(object sender, EventArgs e)
{
StudentRepository repo = new StudentRepository();
var results =
repo.FindAllStudents().Where(
s =>
s.Name == txtSearchQuery.Text ||
s.LastNameFather == txtSearchQuery.Text ||
s.LastNameMother == txtSearchQuery.Text);
dataGridView1.DataSource = results.Select(s => new
{
Codigo = s.StudentId,
Nombre = s.LastNameFather + " " + s.LastNameMother + ", " + s.Name,
Sexo = s.Sex,
Telefono = s.Telephone
}).ToList();
}
如果我尝试执行以下操作,我只能调用EntityCollection集合,因为可能有许多GradeStudent对象与我的学生关联。每年一次
所以我需要得到他X年的成绩
你对如何实现这一点有什么建议吗?如果我理解这个问题-你正在寻找Grade对象(通过GradeStudent中间表)。如果是这种情况,则需要在EF模型中创建NavigationProperty,以将年级与年级学生(*-1)和年级学生与学生(0..1-*)关联起来
至于将数据提取到您的上下文中,这是一个使用RIA的silverlight应用程序吗?如果是这样,服务中的GetStudentsQuery方法将需要包含这些实体,以便数据随Student对象一起返回 如果已知
等级
,则该操作应有效:
StudentRepository repo = new StudentRepository();
var results =
repo.FindAllStudents().Where(
s =>
(s.Name == txtSearchQuery.Text ||
s.LastNameFather == txtSearchQuery.Text ||
s.LastNameMother == txtSearchQuery.Text) &&
s.GradeStudent.Any(gs => gs.Grade = grade));
我有点困惑,等级(X)是你搜索标准的一部分吗?例如,您是否知道参加本次活动的年份,或者您是否需要每年的多份学生记录?