C# LINQ

LINQ stands for Language-INtegrated Query. LINQ integrates the queries directly in C# through a set of extensions to the language. LINQ allows you to write declarative and expressive code that manipulates data efficiently.

This C# LINQ tutorial helps you master LINQ and take your data manipulation skill to the next level.

What you’ll learn:

  • Learn the LINQ basics and how to construct LINQ queries.
  • Explore LINQ to Objects that help you query in-memory data structures such as arrays and lists.
  • Dive into advanced LINQ topics such as parallel LINQ.

By the end of this tutorial series, you will be able to:

  • Write LINQ queries to select, filter, sort, and group data from various sources.
  • Use LINQ to transform data into different formats, such as XML or JSON.
  • Optimize LINQ queries for performance and efficiency.
  • Apply LINQ to your projects to solve real-world data manipulation problems.

Section 1. C# LINQ Basics

LINQ basics help you understand the basics of LINQ that allow you to write more expressive and efficient code for data manipulation.

  • What is LINQ – introduces you to LINQ, how it works, and why you should use it.
  • IEnumerable<T> – learns about the IEnumerable<T> interface.

Section 2. Selecting elements

This section shows you how to use the Select() method to select elements from a sequence.

  • Select() – shows you how to use the Select() method to project each element of a sequence into a new form.
  • SelectMany() – learns how to use the SelectMany() method to transform each item of a sequence into a new sequence and flatten the sequences into a single sequence.

Section 3. Ordering elements

This section shows you how to use various LINQ Extension methods for sorting elements of a sequence by one or more keys in ascending or descending order.

  • OrderBy() – sorts elements of a sequence by a key in ascending order.
  • OrderByDescending() – sorts elements of a sequence in descending order.
  • ThenBy() – sorts elements of a sequence in ascending order by a secondary key, after the sequence has been sorted by the primary key.
  • ThenByDescending() – sorts elements of a sequence in descending order by a secondary key, after the sequence has been sorted by the primary key.

Section 4. Filtering elements

This section shows you how to filter elements of a sequence based on a condition.

  • Where() – shows you how to filter elements of a sequence based on a specified condition.

Section 5. Selecting a single element

This section introduces you to the extension methods for selecting a single element from a sequence.

  • First() – returns the first element in a sequence that satisfies a condition.
  • Last() – returns the last element in a sequence that satisfies a condition.
  • Single() – returns the only element of a sequence that satisfies a condition.

Section 6. Selecting specific elements

This section introduces to you the extension methods for selecting specific elements from a sequence.

  • Distinct() – retrieves unique elements from a sequence.
  • DistinctBy() – learn how to retrieve unique elements of a sequence based on a specified key selector function.
  • Take() – retrieves a specified number of elements from the beginning of a sequence.
  • TakeWhile() – retrieves elements from a sequence as long as a condition is true and skips the remaining elements.
  • TakeLast() – retrieves the last N elements from a sequence.
  • Skip() – skips a specified number of elements and returns the remaining elements in a sequence.
  • SkipWhile() – bypasses elements until a condition is true and returns the remaining elements of a sequence.
  • SkipLast() – skips the last N elements from the end of the sequence and returns the remaining elements after skipping.
  • Chunk() – split a sequence of elements into chunks with a maximum number of elements.

Section 7. Checking if an element contained in a sequence

This section shows you how to check if an element is contained in a sequence.

  • Any() – shows you how to use the Any() method to test if any element in a sequence satisfy a specified condition.
  • All() – guides you on how to use the All() method to test if all elements in a sequence that satisfy a condition.
  • Contains() – returns true if a sequence contains an element or false otherwise.

Section 8. Grouping data

This section shows you how to group elements into groups.

  • GroupBy() – groups elements of a sequence into groups specified by a key.

Section 9. Aggregating data

This section shows you how to aggregate data from a sequence of elements using various extension methods.

  • Count() – learns how to use the Count() method to get the number of elements in a sequence that satisfy a condition.
  • Average() – shows you how to use the Average() method to calculate the average of numbers of a sequence.
  • Sum() – learns how to use the Sum() method to calculate the sum of numbers of a sequence.
  • Min() – finds the lowest value sequence of numbers.
  • Minby() – returns an element with the lowest key value specified by a key selector function.
  • Max() – finds the highest value in a sequence of numbers.
  • MaxBy() – returns an element with the highest key value specified by a key selector function.
  • Aggregate() – performs an operation on the elements in a sequence, taking the result of the previous operation into account.

Section 10. Iterating elements

This section shows you how to iterate elements of a sequence using various extension methods.

  • ForEach() – shows you how to use ForEach() method to perform an action on each element of a sequence.

Section 11. Joining sequences

This section shows you how to combine two sequences by joining them using one or more keys.

  • Inner Join – learns how to perform an inner join a sequence of elements with another based on one or more keys.

Section 12. Set operations

This section teaches you how to apply set operations such as except, intersect, and union to sequences.

  • SequenceEqual – shows you how to compare two sequences if they have the same elements in the same order.
  • Union() – guides you on how to use the Union() method to create a union set of two sequences.
  • UnionBy() – shows you how to use the Union() method to create a union set of two sequences based on a key selector function.
  • Except() – learns how to retrieve elements from a sequence that do not represent another sequence.
  • ExceptBy() – shows you how to retrieve the elements from the first sequence that do not appear in the second sequence by comparing elements using a key.
  • Intersect() – guides you on how to find the common elements of two sequences.
  • IntersectBy() – shows you how to find the intersection of two sequences based on a key selector function.

Section 13. Range, Repeat, and Reverse

  • Range() – shows you how to generate a sequence of integers.
  • Reverse() – guides you on how to use the Reverse() method to generate a sequence of elements in the reversed order.
  • Repeat() – learns how to generate a sequence that repeats an element N times.

Section 14. Parallel LINQ & IAsyncEnumerable<T>

Parallel LINQ (PLINQ) extends LINQ which enables parallel data processing. PLINQ allows you to optimize LINQ queries by automatically distributing the data processing tasks across multiple CPU cores, which helps improve performance when processing large data sets.

  • AsParallel() – shows you how to execute LINQ queries in parallel.
  • IAsyncEnumerable<T> – learn how to use the IAsyncEnumerable<T> to iterate over a sequence asynchronously.