簡體   English   中英

在此 linq 查詢中合並 3 個表

[英]Combine 3 tables in this linq query

我有以下域實體 class 結構,已最小化以便於閱讀。

public partial class Student
{
    public virtual ICollection<StudentUser> StudentUsers { get; set; }
    public bool IsDefaultStudent { get; set }
    ....
}

public partial class StudentUser
{
   public virtual ICollection<StudentUserRole> StudentUsers { get; set; }
   public bool IsDefaultStudentRole { get; set }
   public int? SectionId { get; set; }
   public virtual Section Section { get; set; }
   ....
}

public class StudentUserRole
{
    public int Id { get; set; }
    public bool IsPrimaryStudentRole { get; set }
}

public class Section
{
    public int Id { get; set; }
    public string SectionCode { get; set; }
}

現在,給定一個Student實例,我需要找出IsDefaultStudent=trueStudentUserSectionCodeStudentUserRole的 SectionCode, StudentUser之一,設置IsPrimaryStudentRole=true

請注意,根據功能, Student下只能存在一個這樣的StudentUser

我們需要找出那個StudentUserSectionCode

我可以通過單獨的查詢來完成,但是你能幫我在一個查詢中完成嗎?

提前致謝

這個對我有用。

var primaryStudentUser = context.Student.StudentUsers.FirstOrDefault(su => su.IsDefaultStudentRole && su.StudentUserRoles.Any(sur => sur.IsPrimaryStudentRole));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM