var sales = _salesService.GetSales(parameters)
The list is something like
var listSales = new List<SalesData>();
listSales .Add(new SalesData
{
Name = "Apple",
Quantity = 1000
});
listSales .Add(new SalesData
{
Name = "Banana",
Quantity = 2000
});
listSales .Add(new SalesData
{
Name = "Peach",
Quantity = 1
});
listSales .Add(new SalesData
{
Name = "Mango",
Quantity = 1
});
I want to write a linq query so that I can group the 'Name' as 'Others' if the quantity is less than 1 percent of the total quantity.
So result of the query should be something like
Apple 56
Banana 23
Others 2 -- ( peach's quantity less than than 1% + Mango Quantity less than 1%)
Well, it sounds like first you need to know the total quantity. That's easy enough:
Then you need to work out the cutoff point, which is 1% of it:
For the grouping and summing, I'd probably go in three steps:
So the overall code would be:
You can combine the "group and select" operations, but personally I find the above simpler to read.