LINQ案例查询时 [英] LINQ Case When Query
本文介绍了LINQ案例查询时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在Oracle SQL语法中有一个如下所示的SQL查询,并且我希望它在LINQ中相等.
I have an SQL query like below in Oracle SQL syntax and I want it in LINQ equal.
Select Case When tbl.Id=1 then 1 else NULL End as col1,
Case When tbl.Id=2 then 2 else NULL End as col2,
Case When tbl.Id=3 then 3 else NULL End as col3
From Table1 tbl
推荐答案
You would use the conditional operator in the Select
:
var result =
table1.Select(x => new
{
col1 = x.Id == 1 ? (int?)1 : null,
col3 = x.Id == 2 ? (int?)2 : null,
col3 = x.Id == 3 ? (int?)3 : null
});
这篇关于LINQ案例查询时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文