LINQ Basics
In this page:
LINQ Basics
LINQ (Language Integrated Query) lets you filter, transform, and aggregate collections declaratively using methods like Where, Select, and OrderBy. It's part of System.Linq and works over any IEnumerable<T>. LINQ queries are typically chained together to express data pipelines in a single readable statement.
Note: LINQ methods are lazily evaluated — they don't run until you enumerate the result, e.g. with foreach or .ToList().
Example: LINQ Basics
using System;
using System.Collections.Generic;
using System.Linq;
class Program
{
static void Main(string[] args)
{
List<int> numbers = new List<int> { 1, 2, 3, 4, 5, 6 };
var evenSquares = numbers
.Where(n => n % 2 == 0)
.Select(n => n * n)
.ToList();
foreach (int n in evenSquares)
{
Console.WriteLine(n);
}
}
}
Login to try C/C++/Java/PHP code in the editor
🔒
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: