我有字典
var OrderBy = new Dictionary<string, IQueryable<Something>>
{
(...)
{ "DateDifference", OrderDirection == "asc" ?
query.OrderBy(x => x.Date1.Value.Date - x.Date2.Value.Date) :
query.OrderByDescending(x => x.Date1.Value.Date - x.Date2.Date)},
};
但是当它尝试在Something
上进行评估时会出现异常,该日期中的任何一个等于null
有人知道怎么做吗?
答案 0 :(得分:1)
好,我是黑客
var OrderBy = new Dictionary<string, IQueryable<Something>>
{
(...)
{ "DateDifference", input.OrderDirection == "asc"
?
input.OrderBy(x =>
(x.Date1 == null ? new DateTime(0).Date : x.Date1.Value.Date)
-
(x.Date2 == null ? new DateTime(0).Date : x.Date2.Value.Date))
:
input.OrderByDescending(x =>
(x.Date1 == null ? new DateTime(0).Date : x.Date1.Value.Date)
-
(x.Date2 == null ? new DateTime(0).Date : x.Date2.Value.Date)) },
};