LINQ (Language Integrated Query) Tutorials

This section provides a comprehensive guide to understanding and utilizing LINQ in your .NET applications. LINQ allows you to write queries directly in C# or Visual Basic against various data sources, making data manipulation more intuitive and readable.

What is LINQ?

LINQ stands for Language Integrated Query. It is a powerful feature introduced in .NET Framework 3.5 that adds querying capabilities to .NET languages in a way that is syntactically similar to SQL. LINQ provides a consistent model for querying objects, databases, XML documents, and other forms of data.

Key Benefits of LINQ:

Getting Started with LINQ to Objects

LINQ to Objects allows you to query collections of objects, such as arrays, lists, and dictionaries. This is often the simplest way to begin learning LINQ.

Basic Syntax

LINQ queries typically consist of a query clause (like from, where, select) and query operators.


// Sample data: a list of numbers
List<int> numbers = new List<int> { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };

// LINQ query to select numbers greater than 5
var query = from num in numbers
            where num > 5
            select num;

// Execute the query and print the results
Console.WriteLine("Numbers greater than 5:");
foreach (var n in query)
{
    Console.Write(n + " ");
}
// Expected Output: 9 8 7 6
            

Explanation:

Note: The query variable (query) in the example above is of type IEnumerable<int>. The query is not actually executed until you iterate over the results (e.g., using a foreach loop). This is known as deferred execution.

Common LINQ Query Operators

LINQ provides a rich set of operators to perform various data manipulation tasks.

Filtering with where

The where clause filters elements based on a specified condition.

Ordering with orderby

The orderby clause sorts the elements in ascending or descending order.


var sortedNumbers = from num in numbers
                    orderby num descending
                    select num;

// Output: 9 8 7 6 5 4 3 2 1 0
            

Projection with select

The select clause reshapes the elements. You can select specific properties or create new anonymous types.


// Example with anonymous types
var result = from num in numbers
             where num % 2 == 0
             select new { Original = num, Squared = num * num };

foreach (var item in result)
{
    Console.WriteLine($"Original: {item.Original}, Squared: {item.Squared}");
}
// Expected Output:
// Original: 4, Squared: 16
// Original: 8, Squared: 64
// Original: 6, Squared: 36
// Original: 2, Squared: 4
// Original: 0, Squared: 0
            

Grouping with group by

The group by clause groups elements based on a key.

Joining with join

The join clause combines elements from two sequences based on matching keys.

Pro Tip: Explore the full range of LINQ query operators, including Distinct, Skip, Take, Count, Sum, Average, Min, Max, and aggregate methods. These are fundamental for efficient data processing.

LINQ to SQL

LINQ to SQL is an O/R (Object-Relational) mapping tool that enables you to query SQL Server databases directly using LINQ syntax. It maps database tables and views to classes and their members.

LINQ to XML

LINQ to XML allows you to query and manipulate XML documents using the LINQ paradigm, providing a more object-oriented approach to working with XML data.

LINQ Methods (Method Syntax)

In addition to query syntax, LINQ also supports method syntax using extension methods defined in the System.Linq.Enumerable and System.Linq.Queryable classes.


// Equivalent query using method syntax
var queryMethod = numbers.Where(num => num > 5);

foreach (var n in queryMethod)
{
    Console.Write(n + " ");
}
// Expected Output: 9 8 7 6
            

Method syntax is often preferred for simple queries or when chaining multiple operations.

Next Steps