在Projection.Conditionals为queryover添加多个条件 [英] Adding more than one condition in Projection.Conditionals for queryover

查看:170
本文介绍了在Projection.Conditionals为queryover添加多个条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想写一个以上的条款的情况;是这样的:

I am trying to write a case with more than one when clause; something like this:

...
case
    when 'starks' then 1
    when 'wildlings' then 2
    when 'lannisters' then 3
    Else 0
End
...

我做了一个条件的东西,如

I've done a single conditional before with something like

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



但我无法弄清楚如何添加一个额外的条件/ 子句中有:/
我试图增加额外的外部条件,附加的限制等,但始终与语法错误。

But I can't figure out how to add an extra condition / when clause in there :/ I've tried adding an extra outer conditional, extra restriction etc, but always end up with syntax error..

感谢您的帮助。

推荐答案

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);



这意味着,第三个参数可以再次成为这个条件预测:

And that means, that the third parameter can again be this Conditional projection:

.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()

生成的SQL看起来像:

The generated SQL will look like:

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

这篇关于在Projection.Conditionals为queryover添加多个条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆