C# 将T-SQL转换为LINQ Lambda

C# 将T-SQL转换为LINQ Lambda,c#,sql-server,linq,tsql,lambda,C#,Sql Server,Linq,Tsql,Lambda,如果需要,如何使用LINQ lambda表达式或查询语法编写此查询?我已经试了一段时间了。多重连接使其变得困难。连接的总记录超过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]),

如果需要,如何使用LINQ lambda表达式或查询语法编写此查询?我已经试了一段时间了。多重连接使其变得困难。连接的总记录超过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])
这是我得到的最接近的,但不会编译。我无法从select块中访问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)
        };
你是非常接近-采取一个谓词

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)
        };
你是非常接近-采取一个谓词

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

安装Resharper,它将完成您的工作:)如果您已经“尝试了一段时间”,那么您应该能够告诉我们您尝试了什么,以及出现了什么问题。可能其他人会建议你已经尝试过了…@JonSkeet我已经用我能得到的最接近的方法更新了。安装Resharper,它将完成你的工作:)如果你已经“尝试了一段时间”,那么大概你应该能够告诉我们你尝试了什么,以及哪里出了问题。可能其他人会建议你已经尝试过…@JonSkeet我已经用我能得到的最接近值更新了。当我尝试时,x属性是“se”,而不是“ot”,这是速率字段为k的地方。需要将“新se组”更改为“新ot组”@Grant-啊,是的,我现在明白了。很好的更新。很高兴您得到了解决方案:)当我尝试将x属性设置为“se”而不是“ot”时,速率字段就是k。需要将“新se组”更改为“新ot组”@Grant-啊,是的,我现在明白了。很好的更新。很高兴您找到了解决方案:)