查询时的LINQ案例

时间:2013-09-13 08:08:10

标签: c# .net sql linq oracle

我在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

2 个答案:

答案 0 :(得分:2)

您可以使用Select中的conditional operator

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

答案 1 :(得分:1)

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