它有点哈克,但应该工作。
var dict = new Dictionary<int, List<int>>();
dict.Add(1, new List<int>() { 1, 2 });
dict.Add(2, new List<int>() { 4, 5 });
dict.Add(3, new List<int>() { 1, 7 });
var max = dict.Select(x => new { Key = x.Key, Value = x.Value.Max() }).OrderByDescending(x => x.Value).First().Key;
// returns 3
// Other sample input
dict.Add(1, new List<int>() { 1, 2 });
dict.Add(2, new List<int>() { 4, 7 });
dict.Add(3, new List<int>() { 1, 2 });
// returns 2
dict.Add(1, new List<int>() { 1, 2 });
dict.Add(2, new List<int>() { 4, 7 });
dict.Add(3, new List<int>() { 1, 7 });
// returns 2
dict.Add(1, new List<int>() { 1,10 });
dict.Add(2, new List<int>() { 4, 7 });
dict.Add(3, new List<int>() { 1, 7 });
// returns 1
编辑:于与最大值列表中的最小值:
var min_value_in_maxList = dict.Select(x => new { Key = x.Key, ValueMax = x.Value.Max(), ValueMin = x.Value.Min() }).OrderByDescending(x => x.ValueMax).First().ValueMin;
你能解释在lambda新的关键字。我没有得到那部分。 – 2012-03-08 12:49:59
在这种情况下,new会生成一个具有两个属性Key和Value的匿名类型。看到这个问题的详细答案。 http://stackoverflow.com/questions/48668/how-should-anonymous-types-be-used-in-c – Alex 2012-03-08 12:52:04
还有一个问题,如果我想在列表中找到最少的数字,那么max是当下。 –
2012-03-08 12:55:33