最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

LINQ排序操作符用法

 更新時(shí)間:2022年02月28日 11:46:39   作者:.NET開(kāi)發(fā)菜鳥(niǎo)  
這篇文章介紹了LINQ排序操作符的用法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

Linq中的排序操作符包括OrderBy、OrderByDescending、ThenBy、ThenByDescending和Reverse,提供了升序或者降序排序。

一、OrderBy操作符

OrderBy操作符用于對(duì)輸入序列中的元素進(jìn)行排序,排序基于一個(gè)委托方法的返回值順序。排序過(guò)程完成后,會(huì)返回一個(gè)類(lèi)型為IOrderEnumerable<T>的集合對(duì)象。其中IOrderEnumerable<T>接口繼承自IEnumerable<T>接口。下面來(lái)看看OrderBy的定義:

從上面的截圖中可以看出,OrderBy是一個(gè)擴(kuò)展方法,只要實(shí)現(xiàn)了IEnumerable<T>接口的就可以使用OrderBy進(jìn)行排序。OrderBy共有兩個(gè)重載方法:第一個(gè)重載的參數(shù)是一個(gè)委托類(lèi)型和一個(gè)實(shí)現(xiàn)了IComparer<T>接口的類(lèi)型。第二個(gè)重載的參數(shù)是一個(gè)委托類(lèi)型??纯聪旅娴氖纠?/p>

定義產(chǎn)品類(lèi):

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

namespace OrderOperation
{
    public class Products
    {
        public int Id { get; set; }
        public int CategoryId { get; set; }
        public string Name { get; set; }
        public double Price { get; set; }
        public DateTime CreateTime { get; set; }
    }
}

在Main()方法里面調(diào)用:

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

namespace OrderOperation
{
    class Program
    {
        static void Main(string[] args)
        {
            // 初始化數(shù)據(jù)
            List<Products> listProduct = new List<Products>()
            {
               new Products(){Id=1,CategoryId=1, Name="C#高級(jí)編程第10版", Price=100.67,CreateTime=DateTime.Now},
               new Products(){Id=2,CategoryId=1, Name="Redis開(kāi)發(fā)和運(yùn)維", Price=69.9,CreateTime=DateTime.Now.AddDays(-19)},
               new Products(){Id=3,CategoryId=1, Name="ASP.NET Core", Price=57,CreateTime=DateTime.Now.AddMonths(-3)},
               new Products(){Id=4,CategoryId=1, Name="Entity Framework 6.x", Price=97,CreateTime=DateTime.Now.AddMonths(-1)}
            };
            Console.WriteLine("方法語(yǔ)法");
            // 1、查詢方法,返回匿名類(lèi)
            var list = listProduct.OrderBy(p => p.CreateTime).Select(p => new { id = p.Id, ProductName = p.Name,ProductPrice=p.Price,PublishTime=p.CreateTime }).ToList();
            foreach (var item in list)
            {
                Console.WriteLine($"item:{item}");
            }
            Console.WriteLine("查詢表達(dá)式");
            // 2、查詢表達(dá)式,返回匿名類(lèi)
            var listExpress = from p in listProduct orderby p.CreateTime select new { id = p.Id, ProductName = p.Name, ProductPrice = p.Price, PublishTime = p.CreateTime };
            foreach (var item in listExpress)
            {
                Console.WriteLine($"item:{item}");
            }

            Console.ReadKey();
        }
    }
}

結(jié)果:

從截圖中可以看出,集合按照CreateTime進(jìn)行升序排序。

在來(lái)看看第一個(gè)重載方法的實(shí)現(xiàn):

先定義PriceComparer類(lèi)實(shí)現(xiàn)IComparer<T>接口,PriceComparer類(lèi)定義如下:

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

namespace OrderOperation
{
    public class PriceComparer : IComparer<double>
    {
        public int Compare(double x, double y)
        {
            if (x > y)
            {
                return 1;     //表示x>y
            }
            else if (x < y)
            {
                return -1;    //表示x<y
            }
            else
            {
                return 0;     //表示x=y
            }
        }
    }
}

在Main()方法里面調(diào)用:

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

namespace OrderOperation
{
    class Program
    {
        static void Main(string[] args)
        {
            // 初始化數(shù)據(jù)
            List<Products> listProduct = new List<Products>()
            {
               new Products(){Id=1,CategoryId=1, Name="C#高級(jí)編程第10版", Price=100.67,CreateTime=DateTime.Now},
               new Products(){Id=2,CategoryId=1, Name="Redis開(kāi)發(fā)和運(yùn)維", Price=69.9,CreateTime=DateTime.Now.AddDays(-19)},
               new Products(){Id=3,CategoryId=1, Name="ASP.NET Core", Price=57,CreateTime=DateTime.Now.AddMonths(-3)},
               new Products(){Id=4,CategoryId=1, Name="Entity Framework 6.x", Price=97,CreateTime=DateTime.Now.AddMonths(-1)}
            };
            Console.WriteLine("方法語(yǔ)法");
            // 1、查詢方法,按照價(jià)格升序排序,返回匿名類(lèi)
            var list = listProduct.OrderBy(p => p.Price,new PriceComparer()).Select(p => new { id = p.Id, ProductName = p.Name, ProductPrice = p.Price, PublishTime = p.CreateTime }).ToList();
            foreach (var item in list)
            {
                Console.WriteLine($"item:{item}");
            }
            Console.ReadKey();
        }
    }
}

結(jié)果:

注意:orderby必須在select之前出現(xiàn),查詢表達(dá)式最后只可能出現(xiàn)select或者groupby。

二、OrderByDescending

OrderByDescending操作符的功能與OrderBy操作符基本相同,二者只是排序的方式不同。OrderBy是升序排序,而OrderByDescending則是降序排列。下面看看OrderByDescending的定義:

從方法定義中可以看出,OrderByDescending的方法重載和OrderBy的方法重載一致。來(lái)看下面的例子:

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

namespace OrderOperation
{
    class Program
    {
        static void Main(string[] args)
        {
            // 初始化數(shù)據(jù)
            List<Products> listProduct = new List<Products>()
            {
               new Products(){Id=1,CategoryId=1, Name="C#高級(jí)編程第10版", Price=100.67,CreateTime=DateTime.Now},
               new Products(){Id=2,CategoryId=1, Name="Redis開(kāi)發(fā)和運(yùn)維", Price=69.9,CreateTime=DateTime.Now.AddDays(-19)},
               new Products(){Id=3,CategoryId=1, Name="ASP.NET Core", Price=57,CreateTime=DateTime.Now.AddMonths(-3)},
               new Products(){Id=4,CategoryId=1, Name="Entity Framework 6.x", Price=97,CreateTime=DateTime.Now.AddMonths(-1)}
            };
            // 注意:OrderByDescending的方法語(yǔ)法和查詢表達(dá)式寫(xiě)法有些不同。
            Console.WriteLine("方法語(yǔ)法");
            // 1、查詢方法,按照時(shí)間降序排序,返回匿名類(lèi)
            var list = listProduct.OrderByDescending(p => p.CreateTime).Select(p => new { id = p.Id, ProductName = p.Name, ProductPrice = p.Price, PublishTime = p.CreateTime }).ToList();
            foreach (var item in list)
            {
                Console.WriteLine($"item:{item}");
            }
            Console.WriteLine("查詢表達(dá)式");
            var listExpress = from p in listProduct orderby p.CreateTime descending select new { id = p.Id, ProductName = p.Name, ProductPrice = p.Price, PublishTime = p.CreateTime };
            foreach (var item in list)
            {
                Console.WriteLine($"item:{item}");
            }
            Console.ReadKey();
        }
    }
}

結(jié)果:

從截圖中可以看出:輸出結(jié)果按照時(shí)間降序排序。在來(lái)看看另外一個(gè)重載方法的調(diào)用:

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

namespace OrderOperation
{
    class Program
    {
        static void Main(string[] args)
        {
            // 初始化數(shù)據(jù)
            List<Products> listProduct = new List<Products>()
            {
               new Products(){Id=1,CategoryId=1, Name="C#高級(jí)編程第10版", Price=100.67,CreateTime=DateTime.Now},
               new Products(){Id=2,CategoryId=1, Name="Redis開(kāi)發(fā)和運(yùn)維", Price=69.9,CreateTime=DateTime.Now.AddDays(-19)},
               new Products(){Id=3,CategoryId=1, Name="ASP.NET Core", Price=57,CreateTime=DateTime.Now.AddMonths(-3)},
               new Products(){Id=4,CategoryId=1, Name="Entity Framework 6.x", Price=97,CreateTime=DateTime.Now.AddMonths(-1)}
            };
            Console.WriteLine("方法語(yǔ)法");
            // 1、查詢方法,按照價(jià)格降序排序,返回匿名類(lèi)
            var list = listProduct.OrderByDescending(p => p.Price, new PriceComparer()).Select(p => new { id = p.Id, ProductName = p.Name, ProductPrice = p.Price, PublishTime = p.CreateTime }).ToList();
            foreach (var item in list)
            {
                Console.WriteLine($"item:{item}");
            }
            Console.ReadKey();
        }
    }
}

結(jié)果:

輸出結(jié)果也是按照時(shí)間降序排序。

三、ThenBy排序

ThenBy操作符可以對(duì)一個(gè)類(lèi)型為IOrderedEnumerable<T>,(OrderBy和OrderByDesceding操作符的返回值類(lèi)型)的序列再次按照特定的條件順序排序。ThenBy操作符實(shí)現(xiàn)按照次關(guān)鍵字對(duì)序列進(jìn)行升序排列。下面來(lái)看看ThenBy的定義:

從截圖中可以看出:ThenBy()方法擴(kuò)展的是IOrderedEnumerable<T>,因此ThenBy操作符長(zhǎng)常常跟在OrderBy和OrderByDesceding之后??聪旅娴氖纠?/p>

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

namespace OrderOperation
{
    class Program
    {
        static void Main(string[] args)
        {
            // 初始化數(shù)據(jù)
            List<Products> listProduct = new List<Products>()
            {
               new Products(){Id=1,CategoryId=1, Name="C#高級(jí)編程第10版", Price=100.67,CreateTime=DateTime.Now},
               new Products(){Id=2,CategoryId=1, Name="Redis開(kāi)發(fā)和運(yùn)維", Price=69.9,CreateTime=DateTime.Now.AddDays(-19)},
               new Products(){Id=3,CategoryId=2, Name="活著", Price=57,CreateTime=DateTime.Now.AddMonths(-3)},
               new Products(){Id=4,CategoryId=3, Name="高等數(shù)學(xué)", Price=97,CreateTime=DateTime.Now.AddMonths(-1)}
            };
            // 注意:ThenBy()的方法語(yǔ)法和查詢表達(dá)式寫(xiě)法有些不同。
            Console.WriteLine("方法語(yǔ)法升序排序");
            // 1、查詢方法,按照商品分類(lèi)升序排序,如果商品分類(lèi)相同在按照價(jià)格升序排序 返回匿名類(lèi)
            var list = listProduct.OrderBy(p => p.CategoryId).ThenBy(p=>p.Price).Select(p => new { id = p.CategoryId, ProductName = p.Name, ProductPrice = p.Price, PublishTime = p.CreateTime }).ToList();
            foreach (var item in list)
            {
                Console.WriteLine($"item:{item}");
            }
            Console.WriteLine("查詢表達(dá)式升序排序");
            var listExpress = from p in listProduct orderby p.CategoryId,p.Price select new { id = p.CategoryId, ProductName = p.Name, ProductPrice = p.Price, PublishTime = p.CreateTime };
            foreach (var item in listExpress)
            {
                Console.WriteLine($"item:{item}");
            }
            Console.WriteLine("方法語(yǔ)法降序排序");
            // 1、查詢方法,按照商品分類(lèi)降序排序,如果商品分類(lèi)相同在按照價(jià)格升序排序 返回匿名類(lèi)
            var listDesc = listProduct.OrderByDescending(p => p.CategoryId).ThenBy(p => p.Price).Select(p => new { id = p.CategoryId, ProductName = p.Name, ProductPrice = p.Price, PublishTime = p.CreateTime }).ToList();
            foreach (var item in listDesc)
            {
                Console.WriteLine($"item:{item}");
            }
            Console.WriteLine("查詢表達(dá)式降序排序");
            var listExpressDesc = from p in listProduct orderby p.CategoryId descending , p.Price select new { id = p.CategoryId, ProductName = p.Name, ProductPrice = p.Price, PublishTime = p.CreateTime };
            foreach (var item in listExpressDesc)
            {
                Console.WriteLine($"item:{item}");
            }
            Console.ReadKey();
        }
    }
}

結(jié)果:

四、ThenByDescending

ThenByDescending操作符于ThenBy操作符非常類(lèi)似,只是是按照降序排序,實(shí)現(xiàn)按照次關(guān)鍵字對(duì)序列進(jìn)行降序排列。來(lái)看看ThenByDescending的定義:

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

namespace OrderOperation
{
    class Program
    {
        static void Main(string[] args)
        {
            // 初始化數(shù)據(jù)
            List<Products> listProduct = new List<Products>()
            {
               new Products(){Id=1,CategoryId=1, Name="C#高級(jí)編程第10版", Price=100.67,CreateTime=DateTime.Now},
               new Products(){Id=2,CategoryId=1, Name="Redis開(kāi)發(fā)和運(yùn)維", Price=69.9,CreateTime=DateTime.Now.AddDays(-19)},
               new Products(){Id=3,CategoryId=2, Name="活著", Price=57,CreateTime=DateTime.Now.AddMonths(-3)},
               new Products(){Id=4,CategoryId=3, Name="高等數(shù)學(xué)", Price=97,CreateTime=DateTime.Now.AddMonths(-1)}
            };
            // 注意:ThenByDescending()的方法語(yǔ)法和查詢表達(dá)式寫(xiě)法有些不同。
            Console.WriteLine("方法語(yǔ)法升序排序");
            // 1、查詢方法,按照商品分類(lèi)升序排序,如果商品分類(lèi)相同在按照價(jià)格降序排序 返回匿名類(lèi)
            var list = listProduct.OrderBy(p => p.CategoryId).ThenByDescending(p => p.Price).Select(p => new { id = p.CategoryId, ProductName = p.Name, ProductPrice = p.Price, PublishTime = p.CreateTime }).ToList();
            foreach (var item in list)
            {
                Console.WriteLine($"item:{item}");
            }
            Console.WriteLine("查詢表達(dá)式升序排序");
            var listExpress = from p in listProduct orderby p.CategoryId, p.Price descending select new { id = p.CategoryId, ProductName = p.Name, ProductPrice = p.Price, PublishTime = p.CreateTime };
            foreach (var item in listExpress)
            {
                Console.WriteLine($"item:{item}");
            }
            Console.WriteLine("方法語(yǔ)法降序排序");
            // 1、查詢方法,按照商品分類(lèi)降序排序,如果商品分類(lèi)相同在按照價(jià)格降序排序 返回匿名類(lèi)
            var listDesc = listProduct.OrderByDescending(p => p.CategoryId).ThenByDescending(p => p.Price).Select(p => new { id = p.CategoryId, ProductName = p.Name, ProductPrice = p.Price, PublishTime = p.CreateTime }).ToList();
            foreach (var item in listDesc)
            {
                Console.WriteLine($"item:{item}");
            }
            Console.WriteLine("查詢表達(dá)式降序排序");
            var listExpressDesc = from p in listProduct orderby p.CategoryId descending, p.Price descending select new { id = p.CategoryId, ProductName = p.Name, ProductPrice = p.Price, PublishTime = p.CreateTime };
            foreach (var item in listExpressDesc)
            {
                Console.WriteLine($"item:{item}");
            }
            Console.ReadKey();
        }
    }
}

結(jié)果:

五、Reverse

Reverse操作符用于生成一個(gè)與輸入序列中元素相同,但元素排列順序相反的新序列。下面來(lái)看看Reverse()方法的定義:

public static IEnumerable<TSource> Reverse<TSource>(this IEnumerable<TSource> source)

從方法定義中可以看到,這個(gè)擴(kuò)展方法,不需要輸入?yún)?shù),返回一個(gè)新集合。需要注意的是,Reverse方法的返回值是void??聪旅娴睦樱?/p>

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

namespace ThenBy
{
    class Program
    {
        static void Main(string[] args)
        {
            string[] str = { "A", "B", "C", "D", "E"};
            var query = str.Select(p => p).ToList();
            query.Reverse();
            foreach (var item in query)
            {
                Console.WriteLine(item);
            }

            Console.ReadKey();
        }
    }
}

運(yùn)行效果:

到此這篇關(guān)于LINQ排序操作符的文章就介紹到這了。希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論

百色市| 三河市| 辛集市| 高邑县| 白河县| 白河县| 莱芜市| 司法| 石景山区| 南皮县| 临澧县| 丽江市| 万山特区| 桃江县| 读书| 梨树县| 全州县| 泾阳县| 恭城| 苏州市| 汉阴县| 扎兰屯市| 安福县| 龙州县| 佛坪县| 休宁县| 塔河县| 五大连池市| 滕州市| 武宁县| 乌拉特中旗| 荆州市| 厦门市| 招远市| 马公市| 彰化市| 泉州市| 布尔津县| 于田县| 湖口县| 余庆县|