2017-06-04 65 views
0

我有HashSet<Tuple<string, int>>在第二项中,我有数字(1,18,5,46)。如何检索HashSet的第二项<Tuple <string, int>>

我想获得这些项目中的最大数量。

我是编程新手,请原谅我的问题。

我刚刚找到了一种方法,并希望分享: int max = students.Max(kvp => kvp.Item2);

+0

你尝试过什么?你可以显示你的代码!? –

+0

我将HashSet 添加到第一个项目studentname和第二个项目studentexamresults。 'students.Add(new Tuple (studentName [i],studentExamResult [i))' –

+0

为什么你会为此使用'HashSet'?当你做简单的查找时,'HashSet'表现的非常好。但是如果你必须迭代所有项目,你可以简单地使用'List '。 –

回答

-1

你可以做到这一点

using System; 
using System.Collections.Generic; 
using System.Linq; 

public class Program 
{ 
    public static void Main() 
    { 
     var students = new List<Tuple<string, int>>(); 

     students.Add(new Tuple<string, int>("test", 10)); 
     students.Add(new Tuple<string, int>("test", 10)); 

     Console.Write(students.Max(t => t.Item2)); 
    } 
} 
+0

感谢您的解答。我已经这么做了。 'int max = students.Max(kvp => kvp.Item2);'哪种方式有更好的表现? –

+0

@AlbertoCubas我相信你的方式使用更小的堆空间。 –

+0

@AlbertoCubas我喜欢你的解决方案。 – hardkoded

相关问题