我在sql server中使用以下命令來獲取資料:
Select distinct ConstituentGroupNameId,UserId,CreatedTime from dbo.ConituentRecords
但是我無法通過物體框架來實作它。我想獲得唯一的ConstituentGroupNameId和其他欄位,以及。
uj5u.com熱心網友回復:
你可以簡單地在Linq查詢中使用groupby。它可以達到與T-SQL的distinct操作相同的結果。
首先將你想要區分的欄位分組,你可以得到你創建的組。然后你選擇組中的第一個專案。
最后,你可以得到你所過濾的不同行。
Lambda運算式 :
var boo = ConstituentRecords
.GroupBy(o => new { o.ConituentGroupNameId, o.UserId, o.CreatedTime } )
.Select(g=>g.First())
.ToList()。
或者
查詢運算式 :
var boo = (from V in ConstituentRecords
group v by new { v.ConituentGroupNameId, v.UserId, v.CreatedTime } into g
select g.First()).ToList()。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/320375.html
標籤:
上一篇:CS0246找不到型別或名稱空間名稱'ErrorViewModel'(你是否缺少一個using指令或一個程式集參考?
