I have a list of clients
public class Client
{
public string ClientID { get; set; }
public decimal Amount { get; set; }
}
eg. with the following data
ClientID | Amount
---------+-------
000021354| 200
000021353| 300
000021353| 400
000021352| 100
000021351| 200
000021350| 100
the code:
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 });
I want it to be grouped by ClientID and Sumup by sales. Choose the top 3 (customers with most sales) - the rest should be grouped into "others"
Thus, the result should be:
ClientID | Amount
---------+-------
000021353| 700 #1 (300 + 400)
000021354| 200 #2
000021351| 200 #3
others | 200
But for some reason my grouping is not working:
var Grouped = cList.GroupBy(x => x.ClientID)
.OrderByDescending(x => x.Select( y=> y.Amount).Sum())
.Select(x => x).Take(3);
source
share