I'm writing a method that selects customer records based on a group by with the most recent purchase total equal (passed to the method). I did some search and nothing showed up that helps me to achieve the desired output.
customerList.GroupBy(x=> x.CustomerID },
(key, result) =>
{
var orderedList = result.OrderByDescending(c => c.Date).ToList();
return new Customer
{
CustomerID = orderedList.First().CustomerID,
PurchaseID = orderedList.First().PurchaseID,
Price = orderedList.First().Price,
};
});
CUSTOMERID | PURCHACEID | PRICE | DATE |
---|---|---|---|
1 | 11 | 235 | 01-03-2021 |
1 | 12 | 230 | 01-03-2021 |
1 | 14 | 235 | 05-04-2021 |
1 | 15 | 535 | 06-04-2021 |
1 | 16 | 230 | 07-04-2021 |
If I'm looking for most recent total purchase price of 1000, I should get
CUSTOMERID | PURCHACEID | PRICE | DATE |
---|---|---|---|
1 | 14 | 235 | 05-04-2021 |
1 | 15 | 535 | 06-04-2021 |
1 | 16 | 230 | 07-04-2021 |