Gra*_*ant 0 c# t-sql linq sql-server lambda
How can i write this query in LINQ lambda expression or query syntax if required? I have been trying for a while now. The multiple joins are making it difficult. The total records on the join are over 2000 and the final result should be about 15 records
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])
Run Code Online (Sandbox Code Playgroud)
this is the closest I have got but will not compile. I cannot access the ot property from within the select block.
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)
};
Run Code Online (Sandbox Code Playgroud)
你非常接近 - 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)
};
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
459 次 |
| 最近记录: |