2012-06-26 172 views
1

可能重複:
Linq - Top value form each groupLINQ查詢的前10名與GROUP BY

我有以下結構數據:

var lookupDictionary = new Dictionary<string, Dictionary<string, double>>(); 

我想寫一個Linq查詢,通過對內部字典中的雙精度進行總結和排序,爲我提供外部字典中的前10個鍵ionary。我很好地掌握了SQL中的group by,但很難將其轉換爲Linq。

任何可以演示此類查詢的Linq專家?

+3

http://stackoverflow.com/questions/1575316/linq-top-value-form-each-group – paragy

+1

這將真正讓事情更清晰,如果你可以給你的樣品輸入和預期輸出的一個例子。 –

+0

另外,http://www.WhatHaveYouTried.com/ – cadrell0

回答

2

你需要一組嗎?

var top = lookupDictionary 
    .Select(dict => new { dict.Key, InnerCount = dict.Value.Values.Sum() }) 
    .OrderByDescending(dict => dict.InnerCount) 
    .Take(10); 
1
Dictionary<string, Dictionary<string, double>> lookupDictionary = new Dictionary<string, Dictionary<string, double>>(); 
lookupDictionary.Select(outerKeyPair => new 
{ 
    Key = outerKeyPair.Key, 
    Sum = outerKeyPair.Value.Values.Sum() 
}) 
.OrderByDescending(pair => pair.Sum) 
.Take(10);