OrderByDescending缺少标准

时间:2015-11-30 15:46:14

标签: c# linq

我有一个列表,其中在代码运行时添加了新的整数,之后我想使用linq函数“OrderByDescending”但我的列表似乎不包含我正在寻找的标准。我的问题是我如何添加它,因为以下代码没有完成工作..

List<int> Arr = new List<int>();
Power = 5;
current = 1;
Arr.Add(current);
Arr.Add(Power);
Arr.OrderByDescending(p => p.current).ThenByDescending(p => p.power).First();

我得到http://prntscr.com/98pumi

1 个答案:

答案 0 :(得分:0)

我的水晶球告诉我你可能需要这样的东西:

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

namespace ConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            List<Device> list = new List<Device>(){
                new Device(){Power = 3,Current =2}
                , new Device(){Power = 2, Current = 2}
                , new Device(){Power = 1, Current = 3}
            };

            var sorted = list.OrderByDescending(d=>d.Current).ThenByDescending(d=>d.Power);

            foreach (var d in sorted)
            {
                Console.WriteLine("Current {0}, Power {1}",d.Current,d.Power);
            }
            Console.ReadLine();
        }
    }

    class Device
    {
        public int Power {get;set;}
        public int Current { get; set; }
    }
}

基本上,创建一个类来保存您尝试排序的属性。

相关问题