Adding more than one condition in Projection.Conditionals for queryover(在 Projection.Conditionals 中添加多个条件用于查询)
问题描述
我正在尝试编写一个包含多个 when
子句的案例;像这样:
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();
但我不知道如何在其中添加额外的条件/when
子句:/我尝试添加额外的外部条件、额外的限制等,但总是以语法错误结束..
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 中添加多个条件用于查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 Projection.Conditionals 中添加多个条件用于查询


- 良好实践:如何重用 .csproj 和 .sln 文件来为 CI 创建 2022-01-01
- WebMatrix WebSecurity PasswordSalt 2022-01-01
- C# 中多线程网络服务器的模式 2022-01-01
- 如何用自己压缩一个 IEnumerable 2022-01-01
- MoreLinq maxBy vs LINQ max + where 2022-01-01
- Web Api 中的 Swagger .netcore 3.1,使用 swagger UI 设置日期时间格式 2022-01-01
- 在哪里可以找到使用中的C#/XML文档注释的好例子? 2022-01-01
- 输入按键事件处理程序 2022-01-01
- C#MongoDB使用Builders查找派生对象 2022-09-04
- 带有服务/守护程序应用程序的 Microsoft Graph CSharp SDK 和 OneDrive for Business - 配额方面返回 null 2022-01-01