我在 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
您将在 中使用条件运算符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
});
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)
};