【问题标题】:asp.net mvc many-to-many relationships in linq to sqllinq to sql中的asp.net mvc多对多关系
【发布时间】:2013-09-24 14:12:22
【问题描述】:

我有一个 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; }
}

【问题讨论】:

  • 你能发布包括在这个查询中的每个模型吗?
  • @Fals 更新我的问题

标签: c# asp.net asp.net-mvc linq asp.net-mvc-4


【解决方案1】:

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

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

【讨论】:

  • 非常感谢! @BortHunter 你也可以投票给这个好答案!
猜你喜欢
  • 2011-01-09
  • 2012-10-07
  • 2023-04-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-14
  • 1970-01-01
  • 2011-04-12
相关资源
最近更新 更多