[英]C# Entity Framework - Order by Children of Children
我在孩子的孩子上使用 OrderBy() 失敗。
IQueryable<Conteso.Data.Models.Employee> query
= _context.Employee
.Include(e => e.Person)
.Include(e => e.Jobs).ThenInclude(j => j.EmployeeType)
.Include(e => e.Jobs).ThenInclude(j => j.Absences).ThenInclude(a => a.AbsenceType)
.Where(e => e.Jobs.Any(j => j.Absences.Count > 0));
排序前,Results View 中有很多記錄。 排序后,有 0 個項目。 這是我的排序嘗試。
query = query.OrderBy(e => e.Person.LastNameFirst)
.ThenBy(e => e.Jobs.OrderBy(j => j.Absences.OrderBy(a => a.Date)));
我認為您的問題是您沒有從ThenBy
中選擇密鑰(更多信息: https://docs.microsoft.com/en-us/dotnet/api/system.linq.enumerable.thenby?view=net -6.0 )。 考慮使用.Select()
而不是你的 2 .OrderBy()
。
您也可以只寫: ThenBy(e => e.Jobs)
並確保您的 Job class 實現IEqualityComparer 接口
不能在 order by 中使用 order by
嘗試
query = query.OrderBy(e => e.Person.LastNameFirst)
.ThenBy(e => e.Jobs.Absences.Date);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.