开发者

NHibernate QueryOver order by first non-null value (coalescing)

开发者 https://www.devze.com 2023-03-01 04:59 出处:网络
What I\'m trying 开发者_如何学运维to come up is something that\'s expressed like this: var result = Session.QueryOver<Foo>().OrderBy(f => f.UpdatedAt ?? f.CreatedAt);

What I'm trying 开发者_如何学运维to come up is something that's expressed like this:

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

Sure enough, this doesn't work. Rough equivalent of this in T-SQL is

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

What's the kosher way to do "coalescing" in NHibernate QueryOver?


.OrderBy(Projections.SqlFunction("coalesce",
                                 NHibernateUtil.DateTime,
                                 Projections.Property<Foo>(x => x.UpdatedAt),
                                 Projections.Property<Foo>(x => x.CreatedAt)))


Diego's answer is the way I came up with, but it seemed to be too verbose to me, so I asked a question, and got an excellent answer. Basically, you can register your own extensions and then just do

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

where IfNull is your new extension. I've even submitted an improvement proposal to NH Jira, but got no response yet.

0

精彩评论

暂无评论...
验证码 换一张
取 消