How to use order by and group by in C#?

王林
Release: 2023-08-31 21:45:02
forward
1174 people have browsed it

C#中如何使用order by、group by?

Order by is used to sort the arrays in the ascending or in the descending order

GroupBy operator belong to Grouping Operators category. This operator takes a flat sequence of items, organize that sequence into groups (IGrouping ) based on a specific key and return groups of sequence

Example

class ElectronicGoods { public int Id { get; set; } public string Name { get; set; } public string Category { get; set; } public static List GetElectronicItems() { return new List() { new ElectronicGoods { Id = 1, Name = "Mobile", Category = "Phone"}, new ElectronicGoods { Id = 2, Name = "LandLine", Category = "Phone"}, new ElectronicGoods { Id = 3, Name = "Television", Category = "TV"}, new ElectronicGoods { Id = 4, Name = "Grinder", Category = "Food"}, new ElectronicGoods { Id = 5, Name = "Mixer", Category = "Food"}, }; } } class Program { static void Main() { //Group by var res=ElectronicGoods.GetElectronicItems().GroupBy(x => x.Category).Select(x => new { Key = x.Key, electronicGoods = x.OrderBy(c => c.Name) }); foreach (var group in res) { Console.WriteLine("{0} - {1}", group.Key, group.electronicGoods.Count()); Console.WriteLine("----------"); foreach (var electronicGoods in group.electronicGoods) { Console.WriteLine(electronicGoods.Name + "\t" + electronicGoods.Category); } Console.WriteLine(); Console.WriteLine(); } Console.ReadKey(); } }
Copy after login

Output

Phone - 2 ---------- LandLine Phone Mobile Phone TV - 1 ---------- Television TV Food - 2 ---------- Grinder Food Mixer Food
Copy after login

Order By

的翻译为:

按顺序排列

class ElectronicGoods { public int Id { get; set; } public string Name { get; set; } public string Category { get; set; } public static List GetElectronicItems() { return new List() { new ElectronicGoods { Id = 1, Name = "Mobile", Category = "Phone"}, new ElectronicGoods { Id = 2, Name = "LandLine", Category = "Phone"}, new ElectronicGoods { Id = 3, Name = "Television", Category = "TV"}, new ElectronicGoods { Id = 4, Name = "Grinder", Category = "Food"}, new ElectronicGoods { Id = 5, Name = "Mixer", Category = "Food"}, }; } } class Program { static void Main() { //Order by var res = ElectronicGoods.GetElectronicItems().OrderBy(x => x.Category); foreach (var items in res) { Console.WriteLine(items.Name + "\t" + items.Category); } Console.ReadKey(); } }
Copy after login

Output

Grinder Food Mixer Food Mobile Phone LandLine Phone Television TV
Copy after login

The above is the detailed content of How to use order by and group by in C#?. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!