请问如何获得List<int>集合中,最大的那个数

解决方案 »

  1.   


    List<int> nums = new List<int>{1,2,3,4,5,6,7,8,9,10};
    var result = nums.Where(p=>Max(p));
      

  2.   

    是不是有什么命名空间-----"当前上下文不存在Max "
      

  3.   


    List<int> nums = new List<int>{1,2,3,4,5,6,7,8,9,10};
    var result = nums.Where(p=>p).Max();foreach(int i in result)
    {
       Response.Write("最大值为:"+i.ToString());
    }
      

  4.   

    这个是LINQ语法,你的VS是多少的?
      

  5.   

        
        //按从大到小排序后取最第一个
        class Program
        {
            static void Main(string[] args)
            {
                List<int> list = new List<int> { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
                list.Sort(new CompareMax());
                Console.WriteLine(list[0]);
                Console.ReadLine();
            }
        }
        public class CompareMax : IComparer<int>
        {
            public int Compare(int x, int y)
            {
                return y - x;
            }
        }
      

  6.   

    -----"当前上下文不存在Max " ?
      

  7.   

    不好意思,昨天随便写的,没测试(LINQ语法陌生了,好久不用),下面这个没有问题了,测试过的。List<int> nums = new List<int>{1,2,3,100,4,5,6,7,8,9,10};
    var result = nums.Select(p=>p).Max();
    Response.Write("最大值为:"+result .ToString());
      

  8.   


                集合有Max方法的,直接调用就行了。
                List<int> nums = new List<int> { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
                int maxValue= nums.Max();