14

ItemID (int) および Score (int) プロパティを持つ Hit という (C#) クラスがあります。簡潔にするために、残りの詳細はスキップします。今私のコードでは、次の選択を行う必要がある巨大なリストがあります (新しいリストに): 個々の Hit.ItemID ごとにすべての Hit.Score の合計をスコア順に並べて取得する必要があります。したがって、元のリストに次のアイテムがある場合

ItemID=3, Score=5
ItemID=1, Score=5
ItemID=2, Score=5
ItemID=3, Score=1
ItemID=1, Score=8
ItemID=2, Score=10

結果のリストには、次のものが含まれている必要があります。

ItemID=2, Score=15
ItemID=1, Score=13
ItemID=3, Score=6

誰か助けてくれませんか?

4

2 に答える 2

12
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);
于 2009-05-04T15:26:47.130 に答える
4
IEnumerable<Hit> result = hits.
   GroupBy(hit => hit.ItemID).
   Select(group => new Hit 
                   {
                      ItemID = group.Key,
                      Score = group.Sum(hit => hit.Score)
                   }).
   OrderByDescending(hit => hit.Score);
于 2009-05-04T15:33:36.100 に答える