0% found this document useful (0 votes)
191 views2 pages

LINQ Cheat Sheet

This document is a LINQ cheat sheet that outlines basic LINQ methods such as Where, Select, OrderBy, and Aggregate functions. It provides examples of method syntax and query syntax for filtering, projecting, sorting, and aggregating data. The cheat sheet serves as a quick reference for using LINQ in programming.

Uploaded by

patilsahana36
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
191 views2 pages

LINQ Cheat Sheet

This document is a LINQ cheat sheet that outlines basic LINQ methods such as Where, Select, OrderBy, and Aggregate functions. It provides examples of method syntax and query syntax for filtering, projecting, sorting, and aggregating data. The cheat sheet serves as a quick reference for using LINQ in programming.

Uploaded by

patilsahana36
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

LINQ Cheat Sheet

Basic LINQ Methods

Where - Filters elements based on a condition

list.Where(x => x > 5)

Select - Projects each element into a new form

list.Select(x => x * 2)

OrderBy - Sorts in ascending order

list.OrderBy(x => x.Name)

OrderByDescending - Sorts in descending order

list.OrderByDescending(x => x.Age)

First - Gets the first element

list.First()

FirstOrDefault - Gets first or default (null-safe)

list.FirstOrDefault(x => x.Name == "Sahana")

Single - Returns single element, throws if multiple

list.Single(x => x.Id == 1)

ToList - Converts result to a list

list.Where(x => x > 5).ToList()

Count - Returns number of elements

list.Count(x => x.Age > 20)

Any - Checks if any elements match


LINQ Cheat Sheet

list.Any(x => x.IsActive)

All - Checks if all elements match

list.All(x => x.IsActive)

Distinct - Removes duplicates

list.Distinct()

GroupBy - Groups elements by key

list.GroupBy(x => x.Category)

Join - Joins two sequences (like SQL JOIN)

Sum, Max, Min, Average - Aggregate methods

list.Sum(x => x.Marks)

LINQ Query Syntax Example

Equivalent Method Syntax:

students.Where(s => s.Marks > 80).OrderBy(s => s.Name);

Query Syntax:

from s in students

where s.Marks > 80

orderby s.Name

select s;

You might also like