Language Integrated Query (LINQ) is a powerful feature in C# that allows developers to query data from various sources using a consistent and readable syntax. It simplifies the process of working with data collections by providing an elegant way to manipulate data.
What is LINQ?
LINQ stands for Language Integrated Query, and it provides a way to query data from different sources such as collections, SQL databases, XML documents, and more. LINQ integrates query capabilities directly into C#, which enables you to write queries in a syntax that is both familiar and strongly typed.
Types of LINQ
There are several types of LINQ, including:
- LINQ to Objects: Queries against in-memory collections like arrays or lists.
- LINQ to SQL: Queries against SQL databases.
- LINQ to XML: Queries against XML documents.
- LINQ to Entities: Queries against Entity Framework data models.
LINQ to Objects Example
Let’s start exploring LINQ by querying a collection. Here’s how you can use LINQ to query a list of integers and filter out even numbers:
using System;
using System.Collections.Generic;
using System.Linq;
public class Program
{
public static void Main(string[] args)
{
List numbers = new List() { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
var evenNumbers = from number in numbers
where number % 2 == 0
select number;
Console.WriteLine("Even Numbers:");
foreach (var num in evenNumbers)
{
Console.WriteLine(num);
}
}
}
This code snippet does the following:
- Creates a list of integers.
- Uses a LINQ query to find even numbers from the list.
- Prints the even numbers to the console.
The output of this program will be:
Even Numbers:
2
4
6
8
10
LINQ Method Syntax
Besides the query syntax, LINQ supports method syntax using extension methods. Here’s the same example as above, using method syntax:
public class Program
{
public static void Main(string[] args)
{
List numbers = new List() { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
var evenNumbers = numbers.Where(n => n % 2 == 0);
Console.WriteLine("Even Numbers:");
foreach (var num in evenNumbers)
{
Console.WriteLine(num);
}
}
}
Both versions will produce the same output. The method syntax can be more concise, especially for complex queries.
Common LINQ Operations
Here are some common LINQ operations that you might find useful:
- Where: Filters a sequence based on a predicate.
- Select: Projects each element of a sequence into a new form.
- OrderBy: Sorts the elements of a sequence in ascending order.
- GroupBy: Groups the elements of a sequence.
- Aggregate: Applies an accumulator function over a sequence.
Example of Grouping
Let’s look at an example of grouping:
public class Person
{
public string Name { get; set; }
public string City { get; set; }
}
public class Program
{
public static void Main(string[] args)
{
List people = new List
{
new Person { Name = "John", City = "New York" },
new Person { Name = "Jane", City = "San Francisco" },
new Person { Name = "Joe", City = "New York" },
new Person { Name = "Mary", City = "Chicago" }
};
var groupedPeople = from person in people
group person by person.City;
foreach (var group in groupedPeople)
{
Console.WriteLine($"City: {group.Key}");
foreach (var p in group)
{
Console.WriteLine($" - {p.Name}");
}
}
}
}
The above program groups people by their cities and outputs each group:
City: New York
- John
- Joe
City: San Francisco
- Jane
City: Chicago
- Mary
Conclusion
LINQ in C# revolutionizes data processing by providing an elegant and robust way to query data from different sources. Its integration with the language makes it intuitive and highly versatile.
To learn more about ITER Academy, visit our website. https://iter-academy.com/