我有以下查询,它连接了由StationId连接的RailwayStation对象和ExpenditureData对象的集合,我试图获取前10个StationCargoCodes(Station对象上的一个属性),并且还为前10个对象选择了ExpenditureCost的总和,但是我无法获取正确的语法:
var data = (from s in stations join e in expenditureData on s.stationId
equals e.StationId
orderby e.expenditureAmount descending
select s)
.Sum(e => e.expenditureAmount)
.GroupBy(n => n.StationCargoCode)
.Take(10);
为此,我需要什么LINQ进行分组和求和?
最佳答案
var query = from s in stations
join e in expenditureData
on s.stationId equals e.StationId into se
group se by new
{
s.StationCargoCode, ExpenditureCostSum = se.Sum(x => x.ExpenditureCost)
} into g
orderby g.Key.ExpenditureCostSum descending
select g.Key;
var top10 = query.Take(10).ToList();
关于c# - LINQ查询分组并求和,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/43621878/