Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/308.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何从多对多关系中检索实体列表?_C#_Linq_Entity Framework 4_Many To Many - Fatal编程技术网

C# 如何从多对多关系中检索实体列表?

C# 如何从多对多关系中检索实体列表?,c#,linq,entity-framework-4,many-to-many,C#,Linq,Entity Framework 4,Many To Many,我正在尝试从我的数据库中检索列表 基本上,模式是: GradeParalelo ---- GradeStudent ---- Student 其中GradeStudent表包含GradeParallelo和Student的主键注意:GradeStudent表有一个额外的列来保存整数。这不仅仅是一个关联表。 以下是我迄今为止所尝试的: ColegioDBV2Entities db = new ColegioDBV2Entities(); public List<Student> Fi

我正在尝试从我的数据库中检索
列表

基本上,模式是:

GradeParalelo ---- GradeStudent ---- Student
其中GradeStudent表包含GradeParallelo和Student的主键注意:GradeStudent表有一个额外的列来保存整数。这不仅仅是一个关联表。

以下是我迄今为止所尝试的:

ColegioDBV2Entities db = new ColegioDBV2Entities();
public List<Student> FindAllStudentsFromGradeParalelo(int gradeParaleloId)
{
    return (db.Students
           .Include("GradeStudents")
           .Include("GradeStudents.GradeParalelo")
           .Where<Student>(s => s.StudentId == gradeParaleloId)).ToList();
}
ColegioDBV2Entities db=new ColegioDBV2Entities();
公共列表FindAllStudentsFromGradeParallelo(int-GradeParalleloid)
{
返回(db.Students)
.包括(“年级学生”)
.包括(“GradeStudents.GradeParalelo”)
.其中(s=>s.StudentId==Grade Paralleloid)).ToList();
}
但我选择的每个年级都有一个学生。这是可以理解的,因为我在比较一个学生和一个平行年级的学生这不是我想要的。

有人能建议一种检索此集合的方法吗

我要检索GradeStudent表中GradeParalelo ID为6的所有学生的图像

基本上,以更优雅的形式:

public List<Student> FindAllStudentsFromGradeParalelo(int gradeParaleloId)
{
    List<Student> students = new List<Student>();

    using(GradeStudentRepository repo = new GradeStudentRepository())
    {
        var items = repo.FindAllGradeStudents().Where(g => g.GradeParaleloId == gradeParaleloId);

        StudentRepository studentRepo = new StudentRepository();
        foreach (var o in items)
        {
            students.Add(studentRepo.FindStudent(o.StudentId));
        }
    }

    return students;
}
公共列表FindAllStudentsFromGradeParalelo(int-gradeParaleloId)
{
列出学生=新建列表();
使用(GradeStudentRepository repo=new GradeStudentRepository())
{
var items=repo.FindAllGradeStudents()。其中(g=>g.GradeParaleloId==GradeParaleloId);
StudentRepository studentRepo=新StudentRepository();
foreach(项目中的var o)
{
添加(studentRepo.FindStudent(o.StudentId));
}
}
留学生;
}

这是一个简单的查询,如下所示:

return
    db.Students
        .Where( x => x.GradeParalelo.gradeParaleloId == gradeParaleloId )
        .ToList();

如果您拥有FK权限,只需调用FK,它将在内部链接所有需要的表。

这是一个简单的查询,如下所示:

return
    db.Students
        .Where( x => x.GradeParalelo.gradeParaleloId == gradeParaleloId )
        .ToList();
如果您拥有FK权限,只需调用FK,它将在内部链接所有需要的表。

如何

return db.GradeParaleloes.Single(g => g.GradeParaleloId == gradeParaleloId)
                         .GradeStudents.Select(s => s.Student).ToList();
怎么样

return db.GradeParaleloes.Single(g => g.GradeParaleloId == gradeParaleloId)
                         .GradeStudents.Select(s => s.Student).ToList();

是的,这样可以,但是关联表中有一个额外的列。我认为要让它工作,你只需要有primery键和两个table外键。@Sergio:那不会有任何区别。@SLaks:我想是的,因为我现在有它,我不能说
x=>x.GradeParalelo
,但只能说
x=>x.gradeparalelos
。是的,那会工作,但是在关联表中有一个额外的列。我认为要让它工作,你只需要有primery键和两个table外键。@Sergio:那应该没什么区别。@SLaks:我想是的,因为我现在有它,我不能说
x=>x.GradeParalelo
,而只能说
x=>x.GradeParaleloes
。我似乎无法调用.SelectMany方法,知道为什么吗?这似乎完全错了。单次返回IQueryable和SelectMany只对IEnumerable有效。@Slaks:我必须修改一下你的答案,它才能工作。如果我只是编辑你的答案,这是否符合犹太教教义,或者它是如何工作的?我会接受这个答案,因为它导致了解决方案,但为了完整性,我想发布工作代码。:)当然只是做一个建议的编辑。或者,添加您自己的答案,但接受我的答案。我似乎无法调用.SelectMany方法,知道为什么吗?这似乎是完全错误的。单次返回IQueryable和SelectMany只对IEnumerable有效。@Slaks:我必须修改一下你的答案,它才能工作。如果我只是编辑你的答案,这是否符合犹太教教义,或者它是如何工作的?我会接受这个答案,因为它导致了解决方案,但为了完整性,我想发布工作代码。:)当然只是做一个建议的编辑。或者,添加您自己的答案,但接受我的答案。