![](/img/trans.png)
[英]How to select records of user's history of items, where the date is latest for the items in LINQ to entities lambda?
[英]Linq to select latest records
我有數據結構
對於每件商品,都有其在特定日期以每種貨幣表示的價格記錄。 我需要創建一個查詢來返回每種貨幣的最新價格。
此查詢有效,但返回貨幣 ID 1
的多個Amounts
。 它應該只返回 3 條記錄, 7,8 and 9
,因為這些代表該項目所有貨幣的最新價格。
var q = (from c in db.tblStoreItemPrices where c.ItemID == ID select new { c.CurrencyID, c.Amount });
請忽略所有排序並假設記錄是隨機排序的。
這應該工作:
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 });
解釋:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.