Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to use order by, group by in c#?
The OrderBy and GroupBy operators are essential LINQ methods in C# that allow you to sort and organize data collections. OrderBy sorts sequences in ascending order, while GroupBy organizes flat sequences into groups based on a specified key.
These operators are particularly useful when working with collections of objects that need to be organized or sorted for better data presentation and analysis.
Syntax
Following is the syntax for OrderBy operator −
var result = collection.OrderBy(x => x.PropertyName); var resultDesc = collection.OrderByDescending(x => x.PropertyName);
Following is the syntax for GroupBy operator −
var result = collection.GroupBy(x => x.KeyProperty);
Using GroupBy Operator
The GroupBy operator organizes a flat sequence into groups based on a key selector. Each group contains items that share the same key value −
using System;
using System.Collections.Generic;
using System.Linq;
class ElectronicGoods {
public int Id { get; set; }
public string Name { get; set; }
public string Category { get; set; }
public static List<ElectronicGoods> GetElectronicItems() {
return new List<ElectronicGoods>() {
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 Category and order items within each group by Name
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();
}
}
}
The output of the above code is −
Phone - 2 ---------- LandLine Phone Mobile Phone TV - 1 ---------- Television TV Food - 2 ---------- Grinder Food Mixer Food
Using OrderBy Operator
The OrderBy operator sorts the entire collection based on a specified property in ascending order −
using System;
using System.Collections.Generic;
using System.Linq;
class ElectronicGoods {
public int Id { get; set; }
public string Name { get; set; }
public string Category { get; set; }
public static List<ElectronicGoods> GetElectronicItems() {
return new List<ElectronicGoods>() {
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 Category in ascending order
var res = ElectronicGoods.GetElectronicItems().OrderBy(x => x.Category);
foreach (var items in res) {
Console.WriteLine(items.Name + "\t" + items.Category);
}
}
}
The output of the above code is −
Grinder Food Mixer Food Mobile Phone LandLine Phone Television TV
Using OrderByDescending
For descending order, use OrderByDescending method −
using System;
using System.Collections.Generic;
using System.Linq;
class Program {
static void Main() {
var numbers = new List<int> { 5, 2, 8, 1, 9, 3 };
var ascending = numbers.OrderBy(x => x);
var descending = numbers.OrderByDescending(x => x);
Console.WriteLine("Ascending: " + string.Join(", ", ascending));
Console.WriteLine("Descending: " + string.Join(", ", descending));
}
}
The output of the above code is −
Ascending: 1, 2, 3, 5, 8, 9 Descending: 9, 8, 5, 3, 2, 1
Comparison
| Feature | OrderBy | GroupBy |
|---|---|---|
| Purpose | Sorts collection items | Groups items by key |
| Return Type | IOrderedEnumerable<T> | IEnumerable<IGrouping<K,V>> |
| Item Count | Same as original | Number of distinct keys |
| Use Case | Displaying sorted lists | Categorizing and analyzing data |
Conclusion
The OrderBy operator sorts collections in ascending order, while GroupBy organizes items into groups based on a key. Both operators can be combined to create sorted groups, making them powerful tools for data organization and presentation in C# applications.
