Skip to content

Latest commit

ย 

History

History
116 lines (79 loc) ยท 3.67 KB

File metadata and controls

116 lines (79 loc) ยท 3.67 KB

LINQ Cheat Sheet (.NET)

Quick reference for common LINQ (Language Integrated Query) operations in C#.


๐Ÿ“„ Basic Syntax

var result = from item in collection
             where item.Property == value
             select item;

OR using method syntax:

var result = collection.Where(item => item.Property == value);

๐Ÿ” Filtering

Operation Example
Where list.Where(x => x.Age > 30)
OfType objects.OfType<string>()

๐Ÿ“Š Projection

Operation Example
Select list.Select(x => x.Name)
SelectMany list.SelectMany(x => x.Children)

๐Ÿ”ข Ordering

Operation Example
OrderBy list.OrderBy(x => x.Name)
OrderByDescending list.OrderByDescending(x => x.Score)
ThenBy list.OrderBy(x => x.Name).ThenBy(x => x.Age)

๐Ÿงฎ Aggregation

Operation Example
Count list.Count()
Sum list.Sum(x => x.Amount)
Average list.Average(x => x.Age)
Min / Max list.Min(x => x.Price)

๐Ÿ”— Joining

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) => ...)

๐Ÿ“ฆ Grouping

var groups = list.GroupBy(x => x.Category);

๐Ÿงช Quantifiers

Operation Example
Any list.Any(x => x.IsActive)
All list.All(x => x.IsValid)
Contains list.Contains(value)

๐Ÿ”„ Element Operations

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)

๐Ÿงน Set Operations

Operation Example
Distinct list.Distinct()
Union list1.Union(list2)
Intersect list1.Intersect(list2)
Except list1.Except(list2)

๐Ÿง  Useful Tips

  • 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.