问题描述
我是Linq的新手,传统上来自SQL背景。我正在尝试适应Linq,但语法似乎很复杂,我不知道是否有任何好处。
无论如何...
如何在LInq中写下以下内容?
感谢您的帮助
我尝试了什么:
我有一个总计和一个日期的表 - 我会使用SQL ,
选择年份(Deliverydate)为年,月(Deliverydate)为mnth,sum(TotalCharge)为tCharge
来自MediaSale的
按年份分组(Deliverydate),月份(Deliverydate)
返回以下结果:
Hi,
I am new to Linq, traditionally from a SQL background. I am trying to adapt to Linq, but the syntax seems complex and I don't know whether there is any benefit.
Anyway...
How would i write the below in LInq?
Thanks for your help
What I have tried:
I have table with a total and a date -using SQL I would have used,
select year(Deliverydate) as yr, month(Deliverydate) as mnth, sum(TotalCharge) as tCharge
from MediaSale
group by year(Deliverydate), month(Deliverydate)
which returns the below result:
which returns the below result:
2017 6 472.2500
2017 7 2115.7700
推荐答案
List<Entity> lst = new List<Entity>();
var data = lst.Select(k => new { k.Deliverydate.Year, k.Deliverydate.Month, k.TotalCharge }).GroupBy(x => new { x.Year, x.Month }, (key, group) => new
{
yr = key.Year,
mnth = key.Month,
tCharge = group.Sum(k => k.TotalCharge)
}).ToList();
这篇关于LINQ-按月,年份归还一笔款项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!