oracle 查询时的 LINQ 案例
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18781323/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 01:55:16 来源:igfitidea点击:
LINQ Case When Query
提问by ehsan
I have an SQL query like below in Oracle SQL syntax and I want it in LINQ equal.
我在 Oracle SQL 语法中有一个像下面这样的 SQL 查询,我希望它在 LINQ 中相等。
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
回答by Daniel Hilgarth
You would use the conditional operatorin the Select
:
你可以使用条件运算符中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
});
回答by AgentFire
var items = from item in Table1
select new
{
col1 = item.Id == 1 ? (int?)1 : null,
col3 = item.Id == 2 ? (int?)2 : null,
col3 = item.Id == 3 ? (int?)3 : null)
};