c# - EF : order by 2 fields when they are not null

标签 c# sql entity-framework entity-framework-6

我有一个带有字段的实体:

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/

相关文章:

c# - 为不属于 Code First 上下文的表创建外键

c# - WPF:当某个值更改时重新应用 DataTemplateSelector

c# - 如何让所有用户的 ASP.NET/Core 应用程序崩溃

sql - 在PostgreSQL中,如何获取所有登录 session 的用户,并在访问数据库时获取他们的IP地址和查询?

php - SQL 查询不工作!

mysql - 在sql中检索具有多个条件的同一列

c# - 如何让 Entity Framework 实现模型和数据库同步

c# - WPF 绑定(bind)用户娱乐

c# - 从 .NET 探查器访问 ThreadStatic 字段

entity-framework - 检查 Entity Framework 中是否存在对象的最佳方法?