Learn Programming, Tech & Coding · Free Online Tools

IT Question Answer
Back to C#
C# LINQ Explained with Examples

C# LINQ Explained with Examples

C#1,193 viewsBy Admin
c-sharplinqexamples

What is LINQ?

LINQ (Language Integrated Query) lets you query collections, databases, and XML using a clean, SQL-like syntax directly in C#.

Method Syntax

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

Query Syntax (SQL-like)

var adults = from p in people
             where p.Age >= 18
             orderby p.Name
             select p.Name;

Common LINQ Methods

MethodPurpose
WhereFilter
SelectTransform
OrderBySort
First / FirstOrDefaultGet one
Sum / Count / AverageAggregate

FAQs

Does LINQ work with databases?

Yes — Entity Framework translates LINQ into SQL. More in our C# section.

Method vs query syntax?

Functionally identical. Method syntax is more common and powerful.