1

Possible Duplicate:
Linq - Top value form each group

I have data in the following structure:

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

I'm trying to write a Linq query to give me the Top 10 keys from the outer dictionary by summing up and ordering descending the doubles in the inner dictionary. I have a good grasp on group by in SQL, but struggle converting that to Linq.

Any Linq experts that can demonstrate this type of query?

Community
  • 1
  • 1
Paul
  • 3,725
  • 12
  • 50
  • 86

2 Answers2

2

Do you need a group by?

var top = lookupDictionary
    .Select(dict => new { dict.Key, InnerCount = dict.Value.Values.Sum() })
    .OrderByDescending(dict => dict.InnerCount)
    .Take(10);
Dave Bish
  • 19,263
  • 7
  • 46
  • 63
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);
Servy
  • 202,030
  • 26
  • 332
  • 449