Language: EN

csharp-que-es-linq

What is LINQ and how to use it in C#

LINQ is a set of features introduced in .NET Framework 3.5 that allows us to perform functional compositions that work on iterables.

LINQ queries can be written using either query syntax or method syntax.

LINQ is one of the wonders of C#. It is very powerful. You should get used to using it as soon as you can because it is a gem.

LINQ Syntax

LINQ can be used in two main ways in C#: with query syntax and with method syntax.

Method Syntax

Method syntax uses extension methods and lambdas to create queries.

int[] numbers = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

var evenNumbers = numbers.Where(number => number % 2 == 0);

Both syntaxes are equivalent in functionality, and the choice between one or the other depends on the developer’s preferences and the context of use.

Query Syntax

Query syntax resembles SQL and is more intuitive for those familiar with SQL queries.

int[] numbers = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

var evenNumbers = from number in numbers
                  where number % 2 == 0
                  select number;

Common Operations in LINQ

Filtering

Filtering is done using the Where method.

var evenNumbers = numbers.Where(number => number % 2 == 0);

Projection

Projection transforms data into a new form. The Select method is used for this purpose.

var squares = numbers.Select(number => number * number);

Sorting

Sorting is achieved with the OrderBy and OrderByDescending methods.

var sortedAsc = numbers.OrderBy(number => number);
var sortedDesc = numbers.OrderByDescending(number => number);

Grouping

Grouping is done with the GroupBy method.

var grouped = numbers.GroupBy(number => number % 2 == 0 ? "Even" : "Odd");

Aggregation

Aggregation summarizes the values of a collection using methods like Count, Sum, Average, Min, and Max.

int sum = numbers.Sum();
int count = numbers.Count();
double average = numbers.Average();
int min = numbers.Min();
int max = numbers.Max();

LINQ to Objects

LINQ to Objects allows querying over in-memory collections like arrays and lists. This is the most common and basic way to use LINQ.

List<string> fruits = new List<string> { "Apple", "Banana", "Cherry", "Peach" };

var fruitsWithA = from fruit in fruits
                  where fruit.Contains("a")
                  select fruit;

foreach (var fruit in fruitsWithA)
{
    Console.WriteLine(fruit);
}

LINQ to XML

LINQ to XML allows querying and manipulating XML documents easily.

XDocument xmlDoc = XDocument.Load("fruits.xml");

var fruitsWithA = from fruit in xmlDoc.Descendants("fruit")
                  where fruit.Value.Contains("a")
                  select fruit;

foreach (var fruit in fruitsWithA)
{
    Console.WriteLine(fruit.Value);
}

LINQ to SQL

LINQ to SQL allows interaction with SQL Server databases using LINQ syntax.

DataContext db = new DataContext("connectionString");

var customers = from customer in db.GetTable<Customer>()
                where customer.City == "Madrid"
                select customer;

foreach (var customer in customers)
{
    Console.WriteLine(customer.Name);
}