繁体   English   中英

LINQ to SQL中的ASP.NET MVC多对多关系

[英]asp.net mvc many-to-many relationships in linq to sql

我有一个asp.net mvc项目,其中有搜索过滤器表单。 也有2个表多对多关系,例如学生和大学。 在我的表格中显示大学的下拉列表。 然后从表单到动作视图查看大学的ID,在这里我需要对整个学生表进行排序,以仅通过该大学招生。 另外,我还有另一个下拉列表用于过滤,但是它们的ID存在于Student表中,而不是StudentId中未包含的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; }
}

获得选定大学的Id值后,假设在一个名为universityId的变量中,您可以通过以下查询获取大学的学生:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM