LINQ Queries Tutorial in C#
Introduction to LINQ
Language Integrated Query (LINQ) is a powerful feature in C# that allows developers to write queries directly in the programming language. It provides a consistent way to query data from different sources such as arrays, collections, databases, XML documents, and more. LINQ makes the code more readable and maintainable.
Basic LINQ Query Syntax
A simple LINQ query consists of three parts:
- Data source: The collection or array to query.
- Query creation: The actual query written using LINQ syntax.
- Query execution: The execution of the query to fetch the results.
Example: Querying an array of integers to find even numbers.
int[] numbers = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; // Query creation var evenNumbers = from num in numbers where num % 2 == 0 select num; // Query execution foreach (var num in evenNumbers) { Console.WriteLine(num); }
2 4 6 8 10
Filtering Data
LINQ provides the where
clause to filter data based on a condition. The following example demonstrates how to filter numbers greater than 5.
int[] numbers = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; var filteredNumbers = from num in numbers where num > 5 select num; foreach (var num in filteredNumbers) { Console.WriteLine(num); }
6 7 8 9 10
Ordering Data
LINQ allows ordering data using the orderby
clause. You can sort data in ascending or descending order. Here's an example:
string[] names = { "John", "Jane", "Tom", "Anna", "Emily" }; var sortedNames = from name in names orderby name select name; foreach (var name in sortedNames) { Console.WriteLine(name); }
Anna Emily Jane John Tom
Joining Data
LINQ supports joining data from two different data sources. The join
clause is used to perform inner joins. Here's an example:
var students = new[] { new { StudentId = 1, Name = "John" }, new { StudentId = 2, Name = "Jane" }, new { StudentId = 3, Name = "Tom" } }; var scores = new[] { new { StudentId = 1, Score = 90 }, new { StudentId = 2, Score = 85 }, new { StudentId = 3, Score = 88 } }; var studentScores = from student in students join score in scores on student.StudentId equals score.StudentId select new { student.Name, score.Score }; foreach (var studentScore in studentScores) { Console.WriteLine($"Name: {studentScore.Name}, Score: {studentScore.Score}"); }
Name: John, Score: 90 Name: Jane, Score: 85 Name: Tom, Score: 88
Grouping Data
LINQ allows grouping data using the group
clause. This is useful for aggregating data. Here's an example:
var students = new[] { new { Name = "John", Age = 18 }, new { Name = "Jane", Age = 20 }, new { Name = "Tom", Age = 18 }, new { Name = "Anna", Age = 20 } }; var groupedStudents = from student in students group student by student.Age into ageGroup select ageGroup; foreach (var group in groupedStudents) { Console.WriteLine($"Age Group: {group.Key}"); foreach (var student in group) { Console.WriteLine($" Name: {student.Name}"); } }
Age Group: 18 Name: John Name: Tom Age Group: 20 Name: Jane Name: Anna
LINQ with Lambda Expressions
LINQ queries can also be written using lambda expressions, which are more concise and often preferred by developers. Here's an example:
int[] numbers = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; var evenNumbers = numbers.Where(num => num % 2 == 0); foreach (var num in evenNumbers) { Console.WriteLine(num); }
2 4 6 8 10
Conclusion
LINQ is a powerful tool in C# that simplifies data querying and manipulation. Its consistency and readability make it an essential feature for any C# developer. By mastering LINQ, you can write more efficient and maintainable code.