Skip to main content
 首页 » 编程设计

c#之按可以为空的子记录值排序

2024年02月13日12开发

我已经尝试了一些组合,但我只是不明白如何执行以下操作:

假设我有表Requests 和RequestActivities。我需要按 RequestActivity.TimeOfCreation 降序排列所有请求,但 RequestActivity 可能为空。

      List<DA.GeneralRequest> ongoingGeneralRequests = db.GeneralRequests 
          .Where(t => t.GeneralRequestStatusID != 3 && (t.SupervisorID == currentUserId || t.CreatorID == currentUserId || t.AssignedUsers.Any(au => au.UserID == currentUserId))) 
          .OrderByDescending(x => x.GeneralRequestActivities.OrderBy(ga => ga.GeneralRequestActivityDate).Last().GeneralRequestActivityDate) //gives exeption 
          .ThenBy(a => a.Deadline).ToList(); 

请您参考如下方法:

我不是很熟悉 LINQ-To-SQL 但在这种情况下无法使用 MAX?

.OrderByDescending(x => x.GeneralRequestActivities 
                         .Max(ga => ga.GeneralRequestActivityDate)) 
.ThenBy(a => a.Deadline) 
.ToList();