[英]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=true
的StudentUser
的SectionCode
和StudentUserRole
的 SectionCode, StudentUser
之一,設置IsPrimaryStudentRole=true
。
請注意,根據功能, Student
下只能存在一個這樣的StudentUser
。
我們需要找出那個StudentUser
的SectionCode
。
我可以通過單獨的查詢來完成,但是你能幫我在一個查詢中完成嗎?
提前致謝
這個對我有用。
var primaryStudentUser = context.Student.StudentUsers.FirstOrDefault(su => su.IsDefaultStudentRole && su.StudentUserRoles.Any(sur => sur.IsPrimaryStudentRole));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.