I have the data structure
For each item there is a record of it's price on a certain date in each currency. I need to create a query that returns the most current price for each currency.
This query works, but returns multiple Amounts
for currency ID 1
. It should only return 3 records, 7,8 and 9
as these represent the most up to date prices in all currencies for this item.
var q = (from c in db.tblStoreItemPrices where c.ItemID == ID select new { c.CurrencyID, c.Amount });
Please ignore all ordering and assume that records are randomly ordered.
This should work:
db.tblStoreItemPrices
.Where(c => c.ItemID == ID)
.GroupBy(c => c.CurrencyID)
.Select(g => g.OrderByDescending(c => c.Date).First())
.Select(c => new { c.CurrencyID, c.Amount });
Explanation:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With