0

如果需要,我如何在 LINQ lambda 表达式或查询语法中编写此查询?我已经尝试了一段时间了。多个连接使其变得困难。join总记录超过2000条,最终结果应该是15条左右

select 
    year([date]), 
    month([date]),
    sum(ot.rate)
from s as s 
join cs cs on cs.s_id= s.id
join ot ot on ot.id = cs.o_id
group by
    year([date]), 
    month([date])

这是我得到的最接近但不会编译的。我无法从选择块中访问 ot 属性。

    var query = from se in table_se
        join cs in table_cs on se.Id equals cs.S_Id
        join ot in table_ot on cs.O_Id equals ot.Id
        group se by new { se.Date.Year, se.Date.Month } into grp 
        select new
        {
            grp.Key.Year,
            grp.Key.Month,
            grp.Sum(ot.Rate)
        };
4

1 回答 1

5

你非常接近 -Enumerable.Sum需要一个谓词

var query = from se in table_se
        join cs in table_cs on se.Id equals cs.S_Id
        join ot in table_ot on cs.O_Id equals ot.Id
        group ot by new { se.Date.Year, se.Date.Month } into grp 
        select new
        {
            Year = grp.Key.Year,
            Month = grp.Key.Month,
            Sum = grp.Sum(x => x.Rate)
        };
于 2013-09-12T12:41:35.790 回答