我有一个带有字段的实体:
public partial class Load
{
public DateTime CreatedOn { get; set; }
public DateTime? UpdatedOn { get; set; }
}
我必须通过以下方式订购记录 (DESC): 如果 UpdatedOn 有值,则“查看”此值,否则查看 CreatedOn 值。怎么做?
最佳答案
??
运算符称为空合并运算符。如果操作数不为空,则返回左侧操作数;否则返回右边的操作数
look here
items.OrderBy(x => x.UpdatedOn ?? x.CreatedOn);
或
items.OrderByDescending(x => x.UpdatedOn ?? x.CreatedOn);
关于c# - EF : order by 2 fields when they are not null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52661830/