我有一个客户列表
public class Client
{
public string ClientID { get; set; } // Client
public decimal Amount { get; set; } // Sales $
}
例如,使用以下数据:
ClientID | Amount
---------+-------
000021354| 200
000021353| 300
000021353| 400
000021352| 100
000021351| 200
000021350| 100
代码:
List<Client> cList = new List<Client>();
cList.Add(new Client() { ClientID = "000021354", Amount = 200 });
cList.Add(new Client() { ClientID = "000021353", Amount = 300 });
cList.Add(new Client() { ClientID = "000021353", Amount = 400 });
cList.Add(new Client() { ClientID = "000021352", Amount = 100 });
cList.Add(new Client() { ClientID = "000021351", Amount = 200 });
cList.Add(new Client() { ClientID = "000021350", Amount = 100 });
我希望按照客户ID分组,并对销售额进行求和。选择前3名(销售额最高的客户) - 其余客户应分组为“其他”。
所以结果应该是:
ClientID | Amount
---------+-------
000021353| 700 #1 (300 + 400)
000021354| 200 #2
000021351| 200 #3
others | 200 // (000021352 + 000021350)
但是我的分组不起作用:
var Grouped = cList.GroupBy(x => x.ClientID)
.OrderByDescending(x => x.Select( y=> y.Amount).Sum())
.Select(x => x).Take(3); //how to add "others" ?