如何将SQL查询转换为Linq

时间:2017-02-09 06:16:25

标签: c# sql sql-server linq

我们如何将下面的SQL查询转换为linq(C#)?

SELECT o.Week,
        (select sum(Positive-Negative)
        FROM tbl i
        where i.Week <= o.Week
        )   
    FROM tbl o

我已经尝试但我不确定如何总结这些值

var result = from i in weeklySatisfaction
from o in weeklySatisfaction
where i.Week <= o.Week
select new
{
   Week = o.Week,
   Net = i.Positive - i.Negative
};

1 个答案:

答案 0 :(得分:0)

此linq代码将起作用

    var values = weeklySatisfaction.Select(o => new 
   { 
     Week = o.Week, 
     Net = weeklySatisfaction
                 .Where(i => i.Week < o.Week)
                 .Select(x => x.Positive - x.Negative).Sum() 
   });

享受!