LINQ in C#

Erik Zhou | Apr 6, 2023 min read

In this blog post, I’ll introduce you to LINQ, a powerful feature in C# that can make your life easier when working with data collections.

What is LINQ?

LINQ (Language Integrated Query) is a set of extension methods that allows you to query and manipulate data in a more readable way. It works with both in-memory data (like arrays and lists) and external data sources (such as databases or web services). LINQ provides a consistent query syntax, regardless of the data source, making it easy to switch between different data sources without changing your query logic.

Why Use LINQ?

Here are a few reasons why LINQ can be a game-changer for you:

  1. Readability: LINQ queries are easy to read and understand, thanks to their declarative nature.
  2. Flexibility: You can use LINQ with different types of data sources, which makes it very versatile.
  3. Less Code: Using LINQ often results in less code, which makes your codebase easier to maintain and less error-prone.

Getting Started with LINQ

To start using LINQ, you’ll need to add the following using statement at the beginning of your C# file:

using System.Linq;

Now, let’s look at a simple example to demonstrate how LINQ works. Suppose we have a list of integers and want to find all the odd numbers in that list. Here’s how we can do it with LINQ:

List<int> numbers = new List<int> { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

IEnumerable<int> oddNumbers = from num in numbers
                                where num % 2 != 0
                                select num;

foreach (int oddNum in oddNumbers)
{
    Console.WriteLine(oddNum);
}

In the example above, we used the LINQ query syntax to filter the odd numbers. As you can see LINQ works very similarly to how SQL works. But there’s another way to write LINQ queries, called method syntax. Here’s the same example using method syntax:

IEnumerable<int> oddNumbers = numbers.Where(num => num % 2 != 0);

foreach (int oddNum in oddNumbers)
{
    Console.WriteLine(oddNum);
}

Both query and method syntax have their pros and cons. It’s up to you to decide which one you prefer, based on your specific use case and personal preferences.

Common LINQ Operations

Here’s a list of some common LINQ operations that you’ll likely use frequently:

  • Select: Projects data into a new form.
  • Where: Filters data based on a condition.
  • OrderBy/OrderByDescending: Sorts data in ascending or descending order.
  • GroupBy: Groups data based on a specified key.
  • Join: Combines data from two collections based on a common key.
  • Distinct: Removes duplicate elements from a collection.
  • Count: Counts the number of elements in a collection.
  • Any: Determines if any element in a collection satisfies a condition.
  • All: Determines if all elements in a collection satisfy a condition.

Conclusion

I hope this introduction to LINQ in C# has been helpful! By embracing LINQ in your projects, you can write more readable code.