流利的nHibernate QueryOver SQL'CASE'等效

时间:2011-09-26 08:04:29

标签: sql fluent-nhibernate case queryover

基本上我想要做的就是编写这段SQL:

SELECT
CASE
    WHEN t.type = 'a' THEN
        t.name
    ELSE
        t.otherName
    END
    as "Name"
FROM myTable t
在QueryOver中

1 个答案:

答案 0 :(得分:7)

也许有一些更好的语法可能,但这应该

var result = session.QueryOver<MyEntity>()
    .Select(Projections.Alias(
        Projections.Conditional(Restrictions.Eq("type", 'a'),
            Projections.Property(t => t.name),
            Projections.Property(t => t.othername)),
        "Name"
    )
    .List();