.NET(C#) LINQ ThenBy和ThenByDescending的使用

Linq是Language Integrated Query的简称,它是微软在.NET Framework 3.5里面新加入的特性,用以简化查询查询操作。本文主要介绍.NET(C#) 中Linq的ThenBy和ThenByDescending操作符。

1、ThenBy操作符

ThenBy操作符可以对一个类型为IOrderedEnumerable<T>,(OrderByOrderByDesceding操作符的返回值类型)的序列再次按照特定的条件顺序排序。

例如,

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            List<People> pList = new List<People>();
            People p1 = new People(1, "C", 4);
            People p2 = new People(2, "Java", 7);
            People p3 = new People(3, "Python", 11);
            People p4 = new People(4, "Linux", 1);
            People p5 = new People(5,"CJavaPY",1);
            pList.Add(p1);
            pList.Add(p2);
            pList.Add(p3);
            pList.Add(p4);
            pList.Add(p5);
            List<People> pList1 = new List<People>();
            IEnumerable<People> newList = pList.OrderBy(p => p.Age).ThenBy(p => p.Id);
            foreach (var item in newList)
            {
                Console.WriteLine(item.Name);              
            }
            AgeComparer ac = new AgeComparer();
            IEnumerable<People> newList1 = pList.OrderBy(p => p.Age, ac).ThenBy(p => p.Id);
            Console.WriteLine();
            foreach (var item in newList1)
            {
                Console.WriteLine(item.Name);              
            }
            Console.ReadKey();
        }
    }
    public class People
    {
        public People(int id, string name, int age)
        {
            this.Id = id;
            this.Name = name;
            this.Age = age;
        }
        public int Id
        {
            get;
            set;
        }
        public string Name
        {
            get;
            set;
        }
        public int Age
        {
            get;
            set;
        }
    }
    public class AgeComparer : IComparer<int>
    {
        public int Compare(int a1, int a2)
        {
            if (a1 > a2)
            {
                return (-1);    //表示a1 > a2           
            }
            else if (a1 < a2)
            {
                return (1);    //表示a1 < a2            
            }
            else
            {
                return (0);     //表示a1=a2            
            }
        }
    }
}

2、ThenByDescending操作符

ThenByDescending操作符的功能与ThenBy操作符基本相同,二者只是排序的方式不同,ThenBy是顺序排序,而ThenByDescending则是逆序排序。

例如,

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            List<People> pList = new List<People>();
            People p1 = new People(1, "C", 4);
            People p2 = new People(2, "Java", 7);
            People p3 = new People(3, "Python", 11);
            People p4 = new People(4, "Linux", 1);
            People p5 = new People(5,"CJavaPY",1);
            pList.Add(p1);
            pList.Add(p2);
            pList.Add(p3);
            pList.Add(p4);
            pList.Add(p5);
            List<People> pList1 = new List<People>();
            IEnumerable<People> newList = pList.OrderBy(p => p.Age).ThenBy(p => p.Id);     //倒序
            foreach (var item in newList)
            {
                Console.WriteLine(item.Name);              
            }
            Console.ReadKey();
        }
    }
    public class People
    {
        public People(int id, string name, int age)
        {
            this.Id = id;
            this.Name = name;
            this.Age = age;
        }
        public int Id
        {
            get;
            set;
        }
        public string Name
        {
            get;
            set;
        }
        public int Age
        {
            get;
            set;
        }
    }
}

推荐阅读
cjavapy编程之路首页