Skip to main content

Common LINQ Methods in C#

LINQ (Language Integrated Query) offers a wide set of methods to query and manipulate data in C#. Below are examples of some commonly used LINQ methods:

1. Where

The Where method filters elements based on a condition.

int[] numbers = { 1, 2, 3, 4, 5 };

var result = numbers.Where(num => num > 2).ToList();

foreach (var num in result)
{
Console.WriteLine(num); // Output: 3, 4, 5
}

2. Select

The Select method projects each element of a sequence into a new form.

string[] fruits = { "apple", "banana", "cherry" };

var result = fruits.Select(fruit => fruit.ToUpper()).ToList();

foreach (var fruit in result)
{
Console.WriteLine(fruit); // Output: APPLE, BANANA, CHERRY
}

3. OrderBy

The OrderBy method sorts the elements in ascending order.

string[] fruits = { "apple", "banana", "cherry" };

var result = fruits.OrderBy(fruit => fruit).ToList();

foreach (var fruit in result)
{
Console.WriteLine(fruit); // Output: apple, banana, cherry
}

4. OrderByDescending

The OrderByDescending method sorts the elements in descending order.

int[] numbers = { 5, 2, 7, 1, 3 };

var result = numbers.OrderByDescending(num => num).ToList();

foreach (var num in result)
{
Console.WriteLine(num); // Output: 7, 5, 3, 2, 1
}

5. First

The First method returns the first element of a sequence that satisfies a condition.

int[] numbers = { 1, 2, 3, 4, 5 };

var result = numbers.First(num => num > 2);

Console.WriteLine(result); // Output: 3

6. FirstOrDefault

The FirstOrDefault method returns the first element of a sequence that satisfies a condition, or a default value if no such element is found.

int[] numbers = { 1, 2, 3, 4, 5 };

var result = numbers.FirstOrDefault(num => num > 5);

Console.WriteLine(result); // Output: 0 (default value for int)

7. Any

The Any method determines whether any element of a sequence satisfies a condition.

int[] numbers = { 1, 2, 3, 4, 5 };

var result = numbers.Any(num => num > 3);

Console.WriteLine(result); // Output: True

8. All

The All method determines whether all elements of a sequence satisfy a condition.

int[] numbers = { 1, 2, 3, 4, 5 };

var result = numbers.All(num => num > 0);

Console.WriteLine(result); // Output: True

9. Count

The Count method returns the number of elements in a sequence.

int[] numbers = { 1, 2, 3, 4, 5 };

var result = numbers.Count();

Console.WriteLine(result); // Output: 5

10. Sum

The Sum method calculates the sum of a sequence of numeric values.

int[] numbers = { 1, 2, 3, 4, 5 };

var result = numbers.Sum();

Console.WriteLine(result); // Output: 15

11. Average

The Average method calculates the average of a sequence of numeric values.

int[] numbers = { 1, 2, 3, 4, 5 };

var result = numbers.Average();

Console.WriteLine(result); // Output: 3

12. Min

The Min method returns the minimum value in a sequence.

int[] numbers = { 1, 2, 3, 4, 5 };

var result = numbers.Min();

Console.WriteLine(result); // Output: 1

13. Max

The Max method returns the maximum value in a sequence.

int[] numbers = { 1, 2, 3, 4, 5 };

var result = numbers.Max();

Console.WriteLine(result); // Output: 5

14. Take

The Take method returns a specified number of contiguous elements from the start of a sequence.

int[] numbers = { 1, 2, 3, 4, 5 };

var result = numbers.Take(3).ToList();

foreach (var num in result)
{
Console.WriteLine(num); // Output: 1, 2, 3
}

15. Skip

The Skip method bypasses a specified number of elements in a sequence and then returns the remaining elements.

int[] numbers = { 1, 2, 3, 4, 5 };

var result = numbers.Skip(2).ToList();

foreach (var num in result)
{
Console.WriteLine(num); // Output: 3, 4, 5
}

16. Distinct

The Distinct method returns distinct elements from a sequence by using the default equality comparer to compare values.

int[] numbers = { 1, 2, 2, 3, 4, 4, 5 };

var result = numbers.Distinct();

foreach (var num in result)
{
Console.WriteLine(num); // Output: 1, 2, 3, 4, 5
}

Using LINQ with collections and databases

LINQ is very flexible because it can be applied both to in-memory collections (arrays, lists, dictionaries, etc.) and to external data sources like databases through Entity Framework.


LINQ with in-memory collections

Any collection that implements IEnumerable<T> can be queried with LINQ.

Example with a list

List<int> numbers = new List<int> { 1, 2, 3, 4, 5 };

// Query with query syntax
var query = from num in numbers
where num > 2
orderby num descending
select num;

// Query with method syntax
var methodQuery = numbers
.Where(num => num > 2)
.OrderByDescending(num => num);

foreach (var num in query)
{
Console.WriteLine(num); // Prints: 5, 4, 3
}

Example with dictionary

Dictionary<string, int> students = new Dictionary<string, int>
{
{ "Ana", 18 },
{ "Luis", 22 },
{ "Pedro", 19 }
};

var ofAge = students
.Where(s => s.Value >= 18)
.Select(s => s.Key);

foreach (var name in ofAge)
{
Console.WriteLine(name); // Prints: Ana, Luis, Pedro
}

LINQ with databases

With Entity Framework (EF Core), LINQ allows working with tables as if they were collections.

Basic example

using (var context = new AppDbContext())
{
var students = from e in context.Students
where e.Age > 18
orderby e.Name
select e;

foreach (var student in students)
{
Console.WriteLine($"{student.Name} - {student.Age}");
}
}

Example with method syntax

using (var context = new AppDbContext())
{
var students = context.Students
.Where(e => e.Age > 18)
.OrderBy(e => e.Name)
.ToList();

students.ForEach(e =>
Console.WriteLine($"{e.Name} - {e.Age}")
);
}