【问题标题】:NHibernate QueryOver order by first non-null value (coalescing)NHibernate QueryOver 按第一个非空值排序(合并)
【发布时间】:2011-08-13 20:38:03
【问题描述】:

我想表达的是这样的:

var result = Session.QueryOver<Foo>().OrderBy(f => f.UpdatedAt ?? f.CreatedAt);

果然,这行不通。在 T-SQL 中大致等价于

... order by coalesce(f.UpdatedAt, f.CreatedAt)

在 NHibernate QueryOver 中进行“合并”的洁净方式是什么?

【问题讨论】:

    标签: nhibernate coalesce queryover


    【解决方案1】:
    .OrderBy(Projections.SqlFunction("coalesce",
                                     NHibernateUtil.DateTime,
                                     Projections.Property<Foo>(x => x.UpdatedAt),
                                     Projections.Property<Foo>(x => x.CreatedAt)))
    

    【讨论】:

      【解决方案2】:

      Diego 的回答是我想出的办法,但对我来说似乎过于冗长,所以我问了a question,得到了an excellent answer。基本上,你可以注册自己的扩展,然后就可以了

      .OrderBy(f => f.UpdatedAt.IfNull(f.CreatedAt));
      

      IfNull 是您的新扩展名。我什至已经向 NH Jira 提交了an improvement proposal,但还没有收到任何回复。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2023-04-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-05-15
        相关资源
        最近更新 更多