LINQ in C#: Complete Practical Guide

LINQ (Language Integrated Query) lets you query collections and other data sources using readable C# expressions. It is especially useful when filtering, sorting, projecting, grouping, and searching application data.

What is LINQ?

Instead of writing repetitive loops for every collection operation, LINQ provides standard query operators that describe what you want to do with the data.

var activeEmployees = employees
    .Where(e => e.IsActive)
    .OrderBy(e => e.Name)
    .ToList();

Where: Filter Data

Where keeps only elements that satisfy a condition.

var adults = people.Where(p => p.Age >= 18).ToList();

Select: Transform Data

Select projects each item into another value or object.

var names = employees
    .Select(e => e.Name)
    .ToList();

OrderBy and ThenBy

var sorted = employees
    .OrderBy(e => e.Department)
    .ThenBy(e => e.Name)
    .ToList();

Any, All, and Contains

Use these operators when you need to test conditions rather than retrieve an entire collection.

bool hasManagers = employees.Any(e => e.Role == "Manager");
bool allActive = employees.All(e => e.IsActive);
bool containsId = ids.Contains(employeeId);

FirstOrDefault and SingleOrDefault

FirstOrDefault returns the first matching item or the default value when there is no match. SingleOrDefault is appropriate when the data should contain zero or one matching item; it throws if multiple matches exist.

var employee = employees
    .FirstOrDefault(e => e.Id == 10);

GroupBy

GroupBy is useful for summaries and reports.

var byDepartment = employees
    .GroupBy(e => e.Department)
    .Select(g => new
    {
        Department = g.Key,
        Count = g.Count()
    })
    .ToList();

Join

LINQ can combine related sequences using Join.

var result = employees.Join(
    departments,
    e => e.DepartmentId,
    d => d.Id,
    (e, d) => new { e.Name, Department = d.Name });

Method Syntax vs Query Syntax

LINQ supports both method syntax and query syntax.

var result = from e in employees
             where e.IsActive
             orderby e.Name
             select e;

The equivalent method syntax is:

var result = employees
    .Where(e => e.IsActive)
    .OrderBy(e => e.Name);

Deferred Execution

Many LINQ operators use deferred execution. The query may not run until you enumerate it with operations such as foreach, ToList, or ToArray.

This matters when the source collection changes between creating the query and executing it.

Common LINQ Mistakes

LINQ and Entity Framework Core

LINQ is also commonly used with Entity Framework Core. When a query is translated to SQL, keeping filtering and projection on the queryable source can allow the database to perform the work instead of loading unnecessary rows into memory.

var products = await db.Products
    .Where(p => p.IsActive)
    .Select(p => new { p.Id, p.Name })
    .ToListAsync();

Conclusion

LINQ is a core C# skill for modern .NET development. Start with Where, Select, sorting, and aggregation, then move on to grouping, joins, deferred execution, and database queries.

Continue through the C# Tutorials roadmap for more practical C# guides.

đŸ’Ŧ Comments

Sign in with Google to publish immediately, or comment anonymously and wait for approval.

Comments will appear here when available.