从现有的linq列表中选择前x个字符串

时间:2015-07-06 22:30:16

标签: c# linq

我想要一个新的查询显示前10名(如果有10个CostCenters),添加字符串出现的数量并创建一个新的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

1 个答案:

答案 0 :(得分:0)

如果我理解正确,您需要orderbytake

这样的内容
CostCenter = g.Select(n => n.CostCenter).OrderBy(n => n.SomeField).Take(10)