【发布时间】:2023-03-05 17:55:01
【问题描述】:
我有 LINQ 脚本,其中按键分组可能为空。我得到了正确的结果,现在我想先按 null 排序,然后按降序排序。因为按键分组可以为空,所以我不能检查为 .hasValue!
在以下方法中,我收到了 null 异常,因为我似乎没有按顺序正确处理 null?
var v25 = (from transaction in filteredTransactions
join schedule in schedules on
new { siteId = transaction.LoginSiteID, startDate = transaction.LoginDateTime.Date, payrollNumber = transaction.PayrollNumber } equals
new { siteId = schedule.SiteId, startDate = schedule.StartTime.Value.Date, payrollNumber = schedule.PayrollNumber }
into ezi_s_t
from scheduleTransactions in ezi_s_t.DefaultIfEmpty()
group transaction by scheduleTransactions into groupedScheduleTransactions
select new
{
Schedule = groupedScheduleTransactions.Key,
Transactions = groupedScheduleTransactions.ToList()
}
)
.OrderBy(x=> x.Schedule == null? null : x.Schedule.EziScheduleId)
.ToList();
【问题讨论】:
-
为了防止将
null与其他类型进行比较,只需为Key分配一些与EziScheduleId相同类型的小值,以便在订购时优先 -
我在答案中添加了一个演示
标签: c# .net linq entity-framework-core