LINQ優化 將GroupBy換做Distinct

2022-04-10 02:58:17 字數 528 閱讀 5173

這樣做也是沒有辦法的,我之前專案中,查詢中這樣寫的分組:

1 form t in db.table

2where t.state==true

3 group t by new

4 9 into p

10select

new

導致的就是,資料表中不到1w行資料,經過分組後是752行,但是第一次查詢要將近10秒。

這太不能容忍了,所以思來想去,只有更換為distinct

來解決。

1 (form t in db.table

2where t.state==true

3select

new).distinct();

感覺很好,不到1秒,只是微軟對distinct

的支援不是很好,因為這個去重靠的是每個物件的比對,物件相同則去重,而無法對某乙個欄位去重,很遺憾。

我不知道這樣的做法好不好,但是我真的沒找到其他的解決辦法,也請了解這方面的前輩能給出乙個好的解決辦法,謝謝了。

linq 資料分組group by

var results from p in persons group p.car by p.personid into g select new linq在資料分組時,不會像資料庫group by那樣,表面上只返回group by後的一條資料,而是根據要分組的條件,把資料匯聚成乙個字典,字典的鍵為...

使用linq 進行Group by 查詢

上圖是資料庫表 需求是要統計出來 不合格 top 10 其中 ngcode 200代表合格 其他均不合格 sql 語句 如下 select top 10 ngcode as defectcode count 1 as count from dbo tblpassstationdata where n...

Linq使用Group By經驗總結

學習linq時,經常會遇到linq使用group by問題,這裡將介紹linq使用group by問題的解決方法。1.計數 var q from p in db.products group p by p.categoryid into g select new 語句描述 linq使用group b...