【发布时间】:2018-02-12 20:26:34
【问题描述】:
我是 LINQ 和 Entity framweork 的新手,我被困在一个地方。所以,我有 2 个具有一对多关系的表。比如说大学生和学生。
public class Student
{
[Key]
public int StudentId { get; set; }
public int ParentId {get;set;}
public ICollection<College> Colleges { get; set; }
}
public class College
{
[Key]
public int CollegeId { get; set; }
public bool AdmissionStatus { get; set; }
public string StudentId { get; set; }
public virtual Student Student { get; set; }
}
要求:
第 1 步:我会得到one parent ID (from UI),我需要从Students 表中找到所有具有parentID 的学生。
第 2 步:现在我将获取特定家长的学生或学生 ID 列表,现在我想查询 college table 和 find all the students whose status AdmissionStatus is "True" 中的学生列表。
PS:
- College 只能设置两种状态 Accept = true 或 Reject = false。
- 一旦学生身份被接受,那么任何大学都不能将状态设为“已接受”。
- 因此,许多大学可以继续拒绝候选人,但是一旦大学接受了该身份。那么没有大学将能够拒绝或接受。基本上,该学生不会出现在任何大学。
我不知道如何做到这一点。
public ActionResult Show()
{
var parentId = User.Identity.GetUserId();
var StudentList = _context.Students.Where(r => r.ParentId == parentId).ToList();
// now I want to query the College table now, but I am not sure how to achieve it.
return View();
}
学生列表中的每个 StudentId 都会在 College 表中出现多次。 我只想要那些被接受或 AdmissionStatus = true 的。请指导我。
上述情况是假设性的,请耐心等待。
【问题讨论】:
-
您想获得自己的儿子/女儿被录取的大学列表吗?不清楚你想从问题中得到什么
-
不,我想获取申请状态为“已接受”且属于同一个 parentId 的学生列表
-
这就是为什么我获得特定父母的 StudentId 列表的原因。现在我只想检查那些
-
您的两个对象都没有“应用程序状态”属性
-
为什么
AdmissionStatus是College的属性?学生不应该拥有List<College>属性,因为他适用于其中的许多人吗?
标签: c# asp.net-mvc entity-framework linq asp.net-mvc-5