本文介紹了使用 SUM 和 ORDER BY 進行 Linq 查詢的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
限時送ChatGPT賬號..
我有一個名為 Hit 的 (C#) 類,它具有 ItemID (int) 和 Score (int) 屬性.我跳過其余的細節以保持簡短.現在在我的代碼中,我有一個巨大的列表,我需要在該列表上執行以下選擇(進入一個新列表):我需要為每個單獨的 Hit.ItemID 獲取所有 Hit.Score 的總和,按分數排序.所以如果我在原始列表中有以下項目
I have a (C#) class called Hit with an ItemID (int) and a Score (int) property. I skip the rest of the details to keep it short. Now in my code, I have a huge List on which I need to do the following select (into a new List): I need to get the sum of all Hit.Score's for each individual Hit.ItemID, ordered by Score. So if I have the following items in the original list
ItemID=3, Score=5
ItemID=1, Score=5
ItemID=2, Score=5
ItemID=3, Score=1
ItemID=1, Score=8
ItemID=2, Score=10
結果列表應包含以下內容:
the resulting List should contain the following:
ItemID=2, Score=15
ItemID=1, Score=13
ItemID=3, Score=6
有人可以幫忙嗎?
推薦答案
var q = (from h in hits
group h by new { h.ItemID } into hh
select new {
hh.Key.ItemID,
Score = hh.Sum(s => s.Score)
}).OrderByDescending(i => i.Score);
這篇關于使用 SUM 和 ORDER BY 進行 Linq 查詢的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!