In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces the relevant knowledge of how to use the LINQ sorting operator, the content is detailed and easy to understand, the operation is simple and fast, and has a certain reference value. I believe you will gain something after reading this article on how to use the LINQ sorting operator. Let's take a look at it.
The sort operators in Linq include OrderBy, OrderByDescending, ThenBy, ThenByDescending, and Reverse, which provide ascending or descending sorting.
I. OrderBy operator
The OrderBy operator is used to sort the elements in the input sequence based on the order of return values of a delegate method. When the sorting process is complete, a collection object of type IOrderEnumerable is returned. The IOrderEnumerable interface inherits from the IEnumerable interface. Let's look at the definition of OrderBy:
As you can see from the screenshot above, OrderBy is an extension method, and as long as you implement the IEnumerable interface, you can use OrderBy for sorting. OrderBy has two overloaded methods: the first overloaded parameter is a delegate type and a type that implements the IComparer interface. The second overloaded parameter is a delegate type. Take a look at the following example:
Define product classes:
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;}
Call in the Main () method:
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) {/ / initialization data List listProduct = new List () {new Products () {Id=1,CategoryId=1, Name= "C# Advanced programming Edition 10", Price=100.67,CreateTime=DateTime.Now}, new Products () {Id=2,CategoryId=1, Name= "Redis Development and Operations", 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 ("method syntax"); / / 1, query the method, and return the anonymous class var list = listProduct.OrderBy (p = > p.CreateTime) .Select (p = > new {id = p.Id, ProductName = p.Namepome ProductPriceGramp.PublishTimegramp.CreateTime}). ToList (); foreach (var item in list) {Console.WriteLine ($"item: {item}") } Console.WriteLine ("query expression"); / / 2, query expression, return anonymous class 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 ();}
Results:
As you can see from the screenshot, the collections are sorted in ascending order by CreateTime.
Let's take a look at the implementation of the first overloaded method:
First, define the PriceComparer class to implement the IComparer interface. The PriceComparer class is defined as follows:
Using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace OrderOperation {public class PriceComparer: IComparer {public int Compare (double x, double y) {if (x > y) {return 1; / for x > y} else if (x
< y) { return -1; //表示x 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 ();}
Results:
Note: orderby must appear before select, and the query expression can only end up with select or groupby.
II. OrderByDescending
The function of the OrderByDescending operator is basically the same as that of the OrderBy operator, except that they are sorted differently. OrderBy is in ascending order, while OrderByDescending is in descending order. Let's look at the definition of OrderByDescending:
As you can see from the method definition, OrderByDescending's method overload is consistent with OrderBy's method overload. Take a look at the following example:
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) {/ / initialization data List listProduct = new List () {new Products () {Id=1,CategoryId=1, Name= "C# Advanced programming Edition 10", Price=100.67,CreateTime=DateTime.Now}, new Products () {Id=2,CategoryId=1, Name= "Redis Development and Operations", 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)}} / / Note: the method syntax of OrderByDescending is somewhat different from that of query expressions. Console.WriteLine ("method syntax"); / / 1. Query methods, sorted in descending order of time, and return anonymous class 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 ("query expression"); 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 ();}
Results:
It can be seen from the screenshot that the output is sorted in descending order of time. Let's take a look at another overloaded method call:
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) {/ / initialization data List listProduct = new List () {new Products () {Id=1,CategoryId=1, Name= "C# Advanced programming Edition 10", Price=100.67,CreateTime=DateTime.Now}, new Products () {Id=2,CategoryId=1, Name= "Redis Development and Operations", 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 ("method syntax"); / / 1, query the method, sort by price descending order, and return anonymous class 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 ();}
Results:
The output is also sorted in descending order of time.
III. ThenBy sorting
The ThenBy operator can once again sort a sequence of type IOrderedEnumerable (the return type of the OrderBy and OrderByDesceding operators) in a specific conditional order. The ThenBy operator implements an ascending order of sequences by secondary keyword. Let's look at the definition of ThenBy:
You can see from the screenshot that the ThenBy () method extends IOrderedEnumerable, so the length of the ThenBy operator often follows OrderBy and OrderByDesceding. Look at the following example:
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) {/ / initialization data List listProduct = new List () {new Products () {Id=1,CategoryId=1, Name= "C# Advanced programming Edition 10", Price=100.67,CreateTime=DateTime.Now}, new Products () {Id=2,CategoryId=1, Name= "Redis Development and Operations", Price=69.9 CreateTime=DateTime.Now.AddDays (- 19)}, new Products () {Id=3,CategoryId=2, Name= "alive", Price=57,CreateTime=DateTime.Now.AddMonths (- 3)}, new Products () {Id=4,CategoryId=3, Name= "Advanced Mathematics", Price=97,CreateTime=DateTime.Now.AddMonths (- 1)}} / / Note: the method syntax of ThenBy () is somewhat different from the query expression writing. Console.WriteLine ("method syntax ascending order"); / / 1, query method, sort by commodity category in ascending order, and return anonymous class 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 ("Ascending sort of query expressions"); var listExpress = from p in listProduct orderby p.CategoryId select new 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 ("method syntax descending sort") / / 1. Query method, sort by commodity category in descending order. If commodity category is the same, return anonymous class 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 ("descending sort of query expressions"); 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 ();}
Results:
IV. ThenByDescending
The ThenByDescending operator is very similar to the ThenBy operator, except that the sequence is sorted in descending order and the sequence is sorted by secondary keyword. Take a look at the definition of 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) {/ / initialization data List listProduct = new List () {new Products () {Id=1,CategoryId=1, Name= "C# Advanced programming Edition 10", Price=100.67,CreateTime=DateTime.Now}, new Products () {Id=2,CategoryId=1, Name= "Redis Development and Operations", Price=69.9 CreateTime=DateTime.Now.AddDays (- 19)}, new Products () {Id=3,CategoryId=2, Name= "alive", Price=57,CreateTime=DateTime.Now.AddMonths (- 3)}, new Products () {Id=4,CategoryId=3, Name= "Advanced Mathematics", Price=97,CreateTime=DateTime.Now.AddMonths (- 1)}} / / Note: the method syntax of ThenByDescending () is somewhat different from the query expression writing. Foreach (var item in list) {Console.WriteLine ($"item: {item}");} Console.WriteLine ("Ascending sort of query expressions"); 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 ("method syntax descending sort") Foreach (var item in listDesc) {Console.WriteLine ($"item: {item}");} Console.WriteLine ("descending sort of query expressions"); 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 ();}
Results:
5. Reverse
The Reverse operator is used to generate a new sequence that is the same as the elements in the input sequence, but in reverse order. Let's look at the definition of the Reverse () method:
Public static IEnumerable Reverse (this IEnumerable source)
As you can see from the method definition, this extension method does not require input parameters and returns a new collection. It is important to note that the return value of the Reverse method is void. Look at the following example:
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 ();}
Running effect:
This is the end of the article on how to use the LINQ sorting operator. Thank you for reading! I believe that everyone has a certain understanding of the knowledge of "how to use the LINQ sorting operator". If you want to learn more, you are welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.