我有一个示例列表元组:
List<Tuple<string, int>> List = new List<Tuple<string, int>>();
List.Add(new Tuple<string, int>(storage, 3));
List.Add(new Tuple<string, int>(battery, 2));
List.Add(new Tuple<string, int>(hover, 1));
我如何获得前两个键或n个具有最高值的键并将这些键添加到列表中?
答案 0 :(得分:4)
使用LINQ,您可以让
var l = newCollection2.OrderByDescending(x => x.Value).Take(2).Select(x => x.Key).ToList();