LINQ (Language-Integrated Query) is a powerful feature in C# that provides a consistent syntax for querying and manipulating data from various sources. LINQ extension methods are a set of methods that extend the functionality of different data types, enabling developers to write expressive and concise queries. In this tutorial, we will explore some commonly used LINQ extension methods in C# with detailed examples to demonstrate their usage and benefits.

Section 1: Filtering Data

1.1. Where

The Where method filters a sequence of elements based on a specified condition. It takes a predicate as an argument and returns a new sequence containing elements that satisfy the condition. Example:

1.2. OfType

The OfType method filters a sequence and returns only the elements of a specified type. Example:

Section 2: Transforming Data

2.1. Select

The Select method projects each element of a sequence into a new form based on a specified transformation function. Example:

2.2. SelectMany

The SelectMany method projects each element of a sequence to a new sequence and flattens the resulting sequences into one sequence. Example:

Section 3: Sorting Data

3.1. OrderBy and OrderByDescending

The OrderBy and OrderByDescending methods sort the elements of a sequence in ascending or descending order based on a key. Example:

3.2. ThenBy and ThenByDescending

The ThenBy and ThenByDescending methods are used for secondary sorting. They allow you to specify additional sorting criteria after using OrderBy or OrderByDescending. Example:

3.3. Reverse

The Reverse method reverses the order of the elements in a sequence. Example:

Section 4: Aggregating Data

4.1. Count

The Count method returns the number of elements in a sequence that satisfy a specified condition. Example:

4.2. Sum

The Sum method calculates the sum of the elements in a sequence. Example:

4.3. Average

The Average method calculates the average of the elements in a sequence. Example:

4.4. Max and Min

The Max and Min methods return the maximum and minimum values in a sequence, respectively. Example:

Section 5: Retrieving Elements

5.1. First and FirstOrDefault

The First method returns the first element of a sequence that satisfies a specified condition, while FirstOrDefault returns the first element or a default value if the sequence is empty. Example:

5.2. Last and LastOrDefault

The Last method returns the last element of a sequence that satisfies a specified condition, while LastOrDefault returns the last element or a default value if the sequence is empty. Example:

5.3. Single and SingleOrDefault

The Single method returns the only element of a sequence that satisfies a specified condition, while SingleOrDefault returns the single element or a default value if the sequence is empty or contains more than one element. Example:

5.4. ElementAt and ElementAtOrDefault

The ElementAt method returns the element at a specified index in a sequence, while ElementAtOrDefault returns the element at the specified index or a default value if the index is out of range. Example:

Section 6: Skipping and Taking Elements

6.1. Skip and SkipWhile

The Skip method skips a specified number of elements in a sequence and returns the remaining elements, while SkipWhile skips elements until a specified condition is no longer satisfied. Example:

6.2. Take and TakeWhile

The Take method returns a specified number of elements from the start of a sequence, while TakeWhile returns elements until a specified condition is no longer satisfied. Example:

Section 7: Joining and Grouping Data

7.1. Join

The Join method performs an inner join between two sequences based on a common key. Example:

7.2. GroupBy

The GroupBy method groups elements in a sequence based on a key and returns a sequence of groups. Example:

 

LINQ extension methods in C# provide a powerful and expressive way to query and manipulate data from various sources. By utilizing these methods, you can write concise and readable code to filter, transform, sort, aggregate, retrieve, and join data effortlessly. Understanding the usage of LINQ extension methods will significantly enhance your productivity as a C# developer and simplify complex data operations in your applications.

Leave a Comment