【问题标题】:How to search for list of elements via LINQ Lambda query如何通过 LINQ Lambda 查询搜索元素列表
【发布时间】: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 tablefind all the students whose status AdmissionStatus is "True" 中的学生列表。

PS:

  1. College 只能设置两种状态 Accept = true 或 Reject = false。
  2. 一旦学生身份被接受,那么任何大学都不能将状态设为“已接受”。
  3. 因此,许多大学可以继续拒绝候选人,但是一旦大学接受了该身份。那么没有大学将能够拒绝或接受。基本上,该学生不会出现在任何大学。

我不知道如何做到这一点。

  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 列表的原因。现在我只想检查那些
  • 您的两个对象都没有“应用程序状态”属性
  • 为什么AdmissionStatusCollege 的属性?学生不应该拥有List&lt;College&gt; 属性,因为他适用于其中的许多人吗?

标签: c# asp.net-mvc entity-framework linq asp.net-mvc-5


【解决方案1】:

首先确定你的模型。

一个家长有很多学生,每个学生都申请了很多大学

Parent
{
    public List<Student> Students
}

Student
{
    public List<Application>() Applications
}

Application
{
    public bool Accepted
}

现在你的 linq 很简单

var acceptedChildernOfParent = db.Parents
    .FirstOrDefault(i=>i.Id = parentID) //get the parent in question
    .Students //look at their childern who are students
    .Where(
        s=>s.Application.Any(a=>a.Accepted == true) //only show those with an accepted applcation
    );

当然,EF 会对 sql 进行总哈希。你最好这样做

db.Students.SqlQuery(@"
select 
    s.* 
from parents p
left join
   students s 
on
   s.parentId = p.id

left join 
   applications a
on
   a.studentId = s.id

where 
   p.id = @p0
and
   a.Accepted == true"
, parentID);

【讨论】:

  • 感谢您对我的耐心。我会尝试这个解决方案,我会相应地更新/评论/接受答案
【解决方案2】:

您可以在一个查询中轻松组合这两个条件:

var StudentList = _context.colleges.Where(r => (r.Student.ParentId == parentId)&&(r.AdmissionStatus)).Select(r => r.Student)).ToList();

【讨论】:

  • 真的很抱歉,Admission status 列在大学表中。我编辑了我的问题。
【解决方案3】:

试试这个:

var StudentList = _context.Students.Where(r => r.ParentId == parentId && r.Colleges.Any(c => c.AdmissionStatus == true)).ToList();

【讨论】:

    猜你喜欢
    • 2017-12-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-11
    • 1970-01-01
    相关资源
    最近更新 更多