从现有linq列表中选择top x字符串

本文关键字:选择 top 字符串 列表 linq | 更新日期: 2023-09-27 18:01:14

我希望一个新查询显示前10个(如果甚至有10个CostCenter(,添加字符串出现的数量,并创建一个新的CostCenter列表(当它像这样分组时(。


var xxx = (from t in _list_Costs
                           group t by new { t.Date.Year, t.Date.Month }
                               into g
                               select new
                               {
                                   Year = g.Select(o => o.Date.Year).First(),
                                   Month = g.Select(n => n.Date.Month).First(),
                                   Costs = g.Sum(n => (Decimal)n.Amount),
                                   CostCenter = g.Select(n => n.CostCenter)
                               }).ToList();

结果应该是:

Year: Month: CostCenter: Quantity:
2015 1       Bananas     5
2015 1       Apples      3
2015 2       Bananas     12
2015 2       Apples      6
2015 2       Lizzards    2

从现有linq列表中选择top x字符串

如果我理解正确,你想要orderbytake:这样的东西

CostCenter = g.Select(n => n.CostCenter).OrderBy(n => n.SomeField).Take(10)