C# linq to sql中的asp.net mvc多对多关系

C# linq to sql中的asp.net mvc多对多关系,c#,asp.net,asp.net-mvc,linq,asp.net-mvc-4,C#,Asp.net,Asp.net Mvc,Linq,Asp.net Mvc 4,我有一个asp.NETMVC项目,其中有搜索过滤器表单。也有两个表多对多关系,例如学生和大学。在我的表格中显示大学的下拉列表。然后从表格到行动查看飞行大学的id,在这里我需要对整个学生表进行排序,以获得该大学的学生。另外,我还有另一个用于筛选的下拉列表,但它们的id存在于学生表中,而不是不包含在学生表中的UniversityId。我不知道该怎么做,该怎么做。此时,我的查询如下所示: var model = repository.GetStudents() .Where(x => x.Dro

我有一个asp.NETMVC项目,其中有搜索过滤器表单。也有两个表多对多关系,例如学生和大学。在我的表格中显示大学的下拉列表。然后从表格到行动查看飞行大学的id,在这里我需要对整个学生表进行排序,以获得该大学的学生。另外,我还有另一个用于筛选的下拉列表,但它们的id存在于学生表中,而不是不包含在学生表中的UniversityId。我不知道该怎么做,该怎么做。此时,我的查询如下所示:

var model = repository.GetStudents()
.Where(x => x.DropId == (DropId?? x.DropId) && 
x.DropId1 == (DropId1 ?? x.DropId1) && 
//somewhere here must be expression for UniversityId
).ToList();
有人有什么想法吗

编辑:

public class Student {
public int StudentId { get; set; }
public int DropId { get; set; }
public Drop Drop { get; set; }
public int DropId1 { get; set; }
public Drop1 Drop1 { get; set; }
public ICollection<University> Universities { get; set; }
}

public class University {
public int UniversityId { get; set; }
public string UniversityNameShort { get; set; }
public ICollection<Student> Students { get; set; }
}
公共班级学生{
公共int StudentId{get;set;}
公共int-DropId{get;set;}
公共拖放{get;set;}
公共int DropId1{get;set;}
公共Drop1 Drop1{get;set;}
公立ICollection大学{get;set;}
}
公立大学{
公共int大学ID{get;set;}
公共字符串UniversityNameShort{get;set;}
公共ICollection学生{get;set;}
}

一旦获得所选大学的Id值,比如在名为
universityId的变量中,您可以通过以下查询获得该大学的学生:

repository.GetStudents()
          .Where(x => x.DropId == (DropId?? x.DropId) 
                   && x.DropId1 == (DropId1 ?? x.DropId1)
                   && x.Universities.Any(u => u.UniversityId == universityId)
).ToList();

获得所选大学的Id值后,比如在名为
universityId
的变量中,可以通过以下查询获得该大学的学生:

repository.GetStudents()
          .Where(x => x.DropId == (DropId?? x.DropId) 
                   && x.DropId1 == (DropId1 ?? x.DropId1)
                   && x.Universities.Any(u => u.UniversityId == universityId)
).ToList();

您可以发布此查询中包含的每个模型吗?@Fals更新我的问题您可以发布此查询中包含的每个模型吗?@Fals更新我的问题谢谢@BortHunter你也可以投票给这个好答案!非常感谢@BortHunter你也可以投票给这个好答案!