SQL到Linq_to_SQL查询转换

时间:2014-02-04 08:15:10

标签: linq-to-sql

select LOC,Date,
    SUM(case when (Local='HO' and Type='CASE') then TotalAmt else 0 end) as col1,
    SUM(TotalAmt) as Total
    from TableName
    where Status='APP'
    group by LOC,Date;

如何在linq中转换此SQL查询

1 个答案:

答案 0 :(得分:0)

我不应该这样做,但这应该是这样的。

<YourSourceForTableName>
          .Where(m => m.Status == "APP")
          .GroupBy(m => new {m.Loc, m.Date})
          .Select(g => new {
                  total = g.Sum(x => x.TotalAmt),
                  col1 = g.Where(m => m.Local == "HO" && m.Type == "CASE").Sum(x => (int?)x.TotalAmt) ?? 0
          });