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 two interchangeable forms,

  • Query syntax (query syntax)
  • Method syntax (method syntax).

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

LINQ is one of the wonders of C#. It is very very powerful. You must 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);

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 such as 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 interacting with SQL Server databases using LINQ syntax.

DataContext db = new DataContext("connectionString");

var clients = from client in db.GetTable<Client>()
              where client.City == "Madrid"
              select client;

foreach (var client in clients)
{
    Console.WriteLine(client.Name);
}