Quick reference for common LINQ (Language Integrated Query) operations in C#.
var result = from item in collection
where item.Property == value
select item;
OR using method syntax:
var result = collection.Where(item => item.Property == value);
Operation |
Example |
Where |
list.Where(x => x.Age > 30) |
OfType |
objects.OfType<string>() |
Operation |
Example |
Select |
list.Select(x => x.Name) |
SelectMany |
list.SelectMany(x => x.Children) |
Operation |
Example |
OrderBy |
list.OrderBy(x => x.Name) |
OrderByDescending |
list.OrderByDescending(x => x.Score) |
ThenBy |
list.OrderBy(x => x.Name).ThenBy(x => x.Age) |
Operation |
Example |
Count |
list.Count() |
Sum |
list.Sum(x => x.Amount) |
Average |
list.Average(x => x.Age) |
Min / Max |
list.Min(x => x.Price) |
Operation |
Example |
Join |
list1.Join(list2, a => a.Id, b => b.Id, (a, b) => ...) |
GroupJoin |
categories.GroupJoin(products, c => c.Id, p => p.CategoryId, (c, p) => ...) |
var groups = list.GroupBy(x => x.Category);
Operation |
Example |
Any |
list.Any(x => x.IsActive) |
All |
list.All(x => x.IsValid) |
Contains |
list.Contains(value) |
Operation |
Example |
First / FirstOrDefault |
list.First(x => x.IsReady) |
Single / SingleOrDefault |
list.Single(x => x.Id == 1) |
Last / LastOrDefault |
list.LastOrDefault() |
ElementAt / ElementAtOrDefault |
list.ElementAt(2) |
Operation |
Example |
Distinct |
list.Distinct() |
Union |
list1.Union(list2) |
Intersect |
list1.Intersect(list2) |
Except |
list1.Except(list2) |
- Use
ToList()
or ToArray()
to execute queries immediately.
- LINQ queries are lazy by default — they only execute when enumerated.
- Use
.AsQueryable()
to enable LINQ-to-Entities in EF.