在Projection.Conditionals中添加多个条件以进行查询

Loc*_*rde 3 c# nhibernate conditional-statements queryover

我正在尝试编写一个包含多个when子句的案例; 这样的事情:

...
case
    when 'starks' then 1
    when 'wildlings' then 2
    when 'lannisters' then 3
    Else 0
End
...
Run Code Online (Sandbox Code Playgroud)

我之前做了一个有条件的条件

.OrderBy(Projections.Conditional(
    Restrictions.Where<House>(r => r.Name.IsLike("starks")),
    Projections.Constant(0),
    Projections.Constant(1))).Asc();
Run Code Online (Sandbox Code Playgroud)

但我无法弄清楚如何when在那里添加一个额外的条件/ 子句:/我已经尝试添加额外的外部条件,额外的限制等,但总是最终出现语法错误..

谢谢您的帮助.

Rad*_*ler 8

Projections.Conditional回报IProjection,它的签名是:

/// <summary>
/// Conditionally return the true or false part, dependention on the criterion
/// </summary>
/// <param name="criterion">The criterion.</param><param name="whenTrue">The when true.
///    </param><param name="whenFalse">The when false.</param>
/// <returns/>
public static IProjection Conditional(ICriterion criterion
                                    , IProjection whenTrue
                                    , IProjection whenFalse);
Run Code Online (Sandbox Code Playgroud)

这意味着,第三个参数可以再次成为此条件投影:

.OrderBy
(
    Projections.Conditional(
        Restrictions.Where<House>(r => r.Name.IsLike("starks")),
        Projections.Constant(1),
        Projections.Conditional(
            Restrictions.Where<House>(r => r.Name.IsLike("wildlings")),
            Projections.Constant(2),
            Projections.Conditional(
                Restrictions.Where<House>(r => r.Name.IsLike("lannisters")),
                Projections.Constant(3),
                Projections.Constant(0)
                )
            )
        )
)
.Asc()
Run Code Online (Sandbox Code Playgroud)

生成的SQL将如下所示:

ORDER BY 
(case when this_.Name LIKE 'starks'     then 1 else 
(case when this_.Name LIKE 'wildlings'  then 2 else 
(case when this_.Name LIKE 'lannisters' then 3 else 0 end) end) end) asc
Run Code Online (Sandbox Code Playgroud)