0

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

1 回答 1

3

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

repository.GetStudents()
          .Where(x => x.DropId == (DropId?? x.DropId) 
                   && x.DropId1 == (DropId1 ?? x.DropId1)
                   && x.Universities.Any(u => u.UniversityId == universityId)
).ToList();
于 2013-09-19T14:22:29.280 回答