LINQ Cheat Sheet for C# Developers

Language Integrated Query (LINQ) is one of the most powerful features available in C#. It allows developers to query collections, databases, XML documents, and other data sources using a consistent syntax.

Whether you're working with arrays, lists, Entity Framework Core, or APIs, LINQ can help you write cleaner and more readable code.

This LINQ Cheat Sheet provides practical examples of the most commonly used LINQ methods that every C# developer should know.


Topics Covered
  • Where
  • Select
  • OrderBy
  • OrderByDescending
  • First & FirstOrDefault
  • Any & All
  • Count
  • Distinct
  • GroupBy
  • Join
  • Take & Skip
  • Best Practices

What Is LINQ?

LINQ stands for Language Integrated Query. It provides query capabilities directly within C# and allows developers to manipulate collections using a concise and readable syntax.

LINQ works with:

  • Arrays
  • Lists
  • Dictionaries
  • Entity Framework Core
  • XML Documents
  • DataTables
  • Many other data sources

Sample Data


List<string> names = new()
{
    "John",
    "Alice",
    "Bob",
    "Sarah",
    "James"
};

We'll use similar examples throughout this tutorial.

1. Where() - Filter Data

The Where method filters elements based on a condition.


var result = names
    .Where(name => name.StartsWith("J"));

Output


John
James

Use Where whenever you need to filter collections.

2. Select() - Transform Data

Select projects each element into a new form.


var lengths = names
    .Select(name => name.Length);

4
5
3
5
5

Select is commonly used for creating DTOs and view models.

3. OrderBy() - Sort Ascending


var sorted = names
    .OrderBy(name => name);

Output


Alice
Bob
James
John
Sarah

4. OrderByDescending() - Sort Descending


var sorted = names
    .OrderByDescending(name => name);

Output


Sarah
John
James
Bob
Alice

5. First()

Returns the first matching element.


var first = names.First();

Result:


John
Important: First() throws an exception if no records exist.

6. FirstOrDefault()


var person =
    names.FirstOrDefault(
        x => x == "Mike");

Returns null if no matching item is found.

Safer than using First() in many situations.

7. Any()

Determines whether at least one element matches a condition.


bool exists =
    names.Any(name => name == "Alice");

Result:


True

8. All()

Checks whether all elements satisfy a condition.


bool valid =
    names.All(name => name.Length > 2);

True

9. Count()

Counts elements in a collection.


int total = names.Count();

5

Count can also filter.


int count =
    names.Count(
        name => name.StartsWith("J"));

2

10. Distinct()

Removes duplicate values.


var numbers = new[]
{
    1, 1, 2, 2, 3, 4, 4
};

var unique =
    numbers.Distinct();

1
2
3
4

11. Take()

Returns a specified number of elements.


var firstTwo =
    names.Take(2);

John
Alice

12. Skip()

Skips a specified number of elements.


var remaining =
    names.Skip(2);

Bob
Sarah
James

Skip and Take are commonly used for pagination.

13. GroupBy()

Groups data by a key.


var groups =
    names.GroupBy(
        name => name.Length);

Useful for reporting and data analysis.

14. Join()

Combines data from multiple collections.


var result =
    customers.Join(
        orders,
        c => c.Id,
        o => o.CustomerId,
        (c, o) => new
        {
            c.Name,
            o.OrderNumber
        });

Join is frequently used with Entity Framework Core.

15. Sum()


var numbers = new[]
{
    10, 20, 30
};

int total =
    numbers.Sum();

60

16. Average()


double average =
    numbers.Average();

20

17. Min() and Max()


int smallest =
    numbers.Min();

int largest =
    numbers.Max();

10
30

Method Summary Table

Method Purpose
Where Filter data
Select Transform data
OrderBy Sort ascending
OrderByDescending Sort descending
First Get first item
Any Check existence
Count Count records
Distinct Remove duplicates
GroupBy Group records
Join Combine collections

LINQ Query Syntax Example


var result =
    from name in names
    where name.StartsWith("J")
    orderby name
    select name;

Although method syntax is more common today, query syntax is still useful for complex queries.

Best Practices

  • Prefer FirstOrDefault() when records may not exist.
  • Use Any() instead of Count() > 0.
  • Keep LINQ queries readable.
  • Use Select() to return only required data.
  • Avoid multiple unnecessary enumerations.
  • Use async LINQ methods with Entity Framework Core.

Frequently Asked Questions

What is LINQ used for?

LINQ is used to query, filter, sort, group, and transform data in C#.

Is LINQ slow?

LINQ is highly optimized and suitable for most applications. However, performance should be tested when working with large datasets.

Can LINQ work with databases?

Yes. Entity Framework Core translates LINQ queries into SQL statements.

Should beginners learn LINQ?

Absolutely. LINQ is one of the most important skills for modern C# and .NET development.

Related Tutorials

Conclusion

LINQ is one of the most valuable productivity features available in C#. It helps developers write cleaner, more maintainable code while reducing the amount of manual looping and filtering logic required.

By mastering methods such as Where, Select, OrderBy, GroupBy, Join, Any, and FirstOrDefault, you'll be able to work more efficiently with collections, databases, APIs, and many other data sources throughout your .NET development career.