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
C# Program to Reverse the List of Cities using LINQ
In C#, LINQ (Language Integrated Query) is a powerful feature that allows us to perform queries on various data sources, including arrays, lists, and databases. It provides an efficient and concise way to manipulate data and has become an essential tool for developers. In this article, we will explore how to use LINQ to reverse a list of cities in C#.
LINQ is a set of extensions to the .NET Framework that provides a standard way to query data from different data sources using a common syntax. It allows developers to write queries that resemble SQL statements, making it easier to manipulate data in a more readable and concise way.
Syntax
Following is the syntax for using LINQ's Reverse() method
IEnumerable<T> reversedSequence = collection.Reverse();
For lists specifically, you can use
var reversedCities = cities.Reverse(); // Direct usage var reversedCities = cities.AsEnumerable().Reverse(); // Explicit conversion
Using LINQ Reverse() Method
To reverse a list of cities using LINQ, we use the Reverse() extension method. This method creates a new sequence with the elements in reverse order without modifying the original collection
Example
using System;
using System.Collections.Generic;
using System.Linq;
class Program {
static void Main() {
List<string> cities = new List<string> {"New York", "Paris", "Tokyo", "London"};
var reversedCities = cities.Reverse();
Console.WriteLine("Original order:");
foreach (var city in cities) {
Console.WriteLine(city);
}
Console.WriteLine("\nReversed order:");
foreach (var city in reversedCities) {
Console.WriteLine(city);
}
}
}
The output of the above code is
Original order: New York Paris Tokyo London Reversed order: London Tokyo Paris New York
Using ToList() with Reverse()
If you need to materialize the reversed sequence into a new list, you can chain the ToList() method
Example
using System;
using System.Collections.Generic;
using System.Linq;
class Program {
static void Main() {
List<string> cities = new List<string> {"Mumbai", "Delhi", "Bangalore", "Chennai"};
List<string> reversedCityList = cities.Reverse().ToList();
Console.WriteLine("Reversed cities as new List:");
for (int i = 0; i < reversedCityList.Count; i++) {
Console.WriteLine($"{i + 1}. {reversedCityList[i]}");
}
}
}
The output of the above code is
Reversed cities as new List: 1. Chennai 2. Bangalore 3. Delhi 4. Mumbai
Using Reverse() with Method Chaining
LINQ methods can be chained together for more complex operations. Here's an example that filters and reverses cities
Example
using System;
using System.Collections.Generic;
using System.Linq;
class Program {
static void Main() {
List<string> cities = new List<string> {"New York", "Paris", "Tokyo", "London", "Berlin", "Rome"};
var filteredAndReversed = cities
.Where(city => city.Length > 5)
.Reverse();
Console.WriteLine("Cities with more than 5 characters (reversed):");
foreach (var city in filteredAndReversed) {
Console.WriteLine(city);
}
}
}
The output of the above code is
Cities with more than 5 characters (reversed): London New York
Conclusion
LINQ's Reverse() method provides an efficient and readable way to reverse the order of elements in a collection. It creates a new sequence without modifying the original collection, making it perfect for functional programming approaches and method chaining scenarios in C#.
