LINQ (Language Integrated Query)

LINQ (Language-Integrated Query) is a powerful and flexible feature in C# that enables you to write queries directly within your C# code against various data sources. It provides a consistent way to query information from objects, databases, XML documents, and more.

What is LINQ?

LINQ unifies querying by providing a common syntax and model. Instead of learning different query languages for different data sources (like SQL for databases, XQuery for XML), you can use LINQ's declarative syntax in C# to query any data that implements the necessary interfaces.

Key Concepts

LINQ Query Syntax vs. Method Syntax

LINQ supports two primary syntaxes for writing queries:

Example: Query Syntax

Query Syntax Example:

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

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

foreach (var n in evenNumbers)
{
    Console.WriteLine(n);
}
                

Example: Method Syntax

Method Syntax Example:

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

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

foreach (var n in evenNumbers)
{
    Console.WriteLine(n);
}
                

Common LINQ Operators

Where()
Filters a sequence of values based on a predicate.
Select()
Projects each element of a sequence into a new form.
OrderBy() / OrderByDescending()
Sorts the elements of a sequence in ascending or descending order.
GroupBy()
Groups the elements of a sequence according to a specified key.
Join()
Correlates elements of two sequences based on matching keys.
SelectMany()
Projects each element of a sequence to an IEnumerable<T> and flattens the resulting sequences into one sequence.
First() / FirstOrDefault()
Returns the first element of a sequence, or a default value if the sequence is empty.
Count()
Returns the number of elements in a sequence.
Note: LINQ to Objects operates on in-memory collections, while LINQ to SQL, LINQ to Entities, and LINQ to XML translate LINQ queries into database-specific or XML-specific queries.

LINQ to Objects

This is the most common form of LINQ, where queries are performed on in-memory collections like arrays, lists, and dictionaries. It leverages the IEnumerable<T> interface.

LINQ to SQL / Entity Framework

LINQ to SQL and Entity Framework allow you to query relational databases using LINQ. The LINQ provider translates your LINQ queries into SQL statements that are executed against the database. This provides type safety and reduces the need for string-based SQL queries.

LINQ to XML

LINQ to XML provides a way to query XML documents programmatically using LINQ. It simplifies XML manipulation and querying compared to traditional DOM-based approaches.

Benefits of LINQ

LINQ is an essential part of modern C# development, significantly enhancing how you interact with data.