✨ Shield now has support for Avalonia UI

C# LINQ: Collections and Data Structures

Apr 5, 2023 | .NET, C#

In this article, we will dive deep into LINQ and its capabilities when working with collections and data structures in C#. We will explore various LINQ methods and techniques to manipulate arrays, lists, and dictionaries effectively. So, let’s get started and become masters of C# LINQ in the world of collections and data structures!

Introduction to LINQ with Collections

Collections are an essential part of any programming language, and C# is no exception. LINQ (Language Integrated Query) is a powerful feature in C# that allows you to query and manipulate collections in a concise and expressive way.

In this section, we will cover the basics of LINQ with collections and see how it can help us work with arrays, lists, and dictionaries more efficiently.

Using LINQ with Arrays

Arrays are the most basic data structure in C#, and LINQ can be used to perform various operations on them, such as filtering, projecting, and aggregating values. Here is an example of using LINQ with an array of integers:

int[] numbers = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

// Filter even numbers using LINQ
var evenNumbers = numbers.Where(n => n % 2 == 0);

// Output: 2, 4, 6, 8, 10
foreach (var num in evenNumbers)
{
    Console.WriteLine(num);
}

Working with Lists

Lists in C# are dynamic arrays that can grow or shrink in size. LINQ can be used to query and manipulate lists, just like arrays. Here is an example of using LINQ with a list of strings:

List<string> names = new List<string> { "Alice", "Bob", "Charlie", "David", "Eve" };

// Filter names starting with 'D' using LINQ
var namesStartingWithD = names.Where(name => name.StartsWith("D"));

// Output: David
foreach (var name in namesStartingWithD)
{
    Console.WriteLine(name);
}

Manipulating Dictionaries

Dictionaries in C# are collections of key-value pairs, useful for storing and retrieving data based on keys. LINQ can also be used to query and manipulate dictionaries. Here’s an example of using LINQ with a dictionary:

Dictionary<int, string> idToName = new Dictionary<int, string>
{
    { 1, "Alice" },
    { 2, "Bob" },
    { 3, "Charlie" },
    { 4, "David" },
    { 5, "Eve" }
};

// Filter entries with keys greater than 3 using LINQ
var filteredEntries = idToName.Where(entry => entry.Key > 3);

// Output: 4: David, 5: Eve
foreach (var entry in filteredEntries)
{
    Console.WriteLine($"{entry.Key}: {entry.Value}");
}

Advanced LINQ Techniques for Collections

Now that we have covered the basics of using LINQ with collections let’s dive into some more advanced techniques that can help us manipulate and query data more effectively.

Projection and Transformation

Projection is a powerful feature of LINQ that allows you to transform the elements of a collection into a new form. The Select method is used for projection, and it takes a lambda expression as an argument to define the transformation. Here’s an example:

int[] numbers = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

// Project square of each number using LINQ
var squaredNumbers = numbers.Select(n => n * n);

// Output: 1, 4, 9, 16, 25, 36, 49, 64, 81, 100
foreach (var square in squaredNumbers)
{
    Console.WriteLine(square);
}

Aggregation and Reduction

LINQ provides several aggregation methods that allow you to reduce a collection to a single value. Some common aggregation methods are Sum, Average, Min, Max, and Count. Here’s an example of using aggregation methods with a list of integers:

List<int> numbers = new List<int> { 1, 3, 5, 7, 9 };

// Calculate the sum, average, min, max, and count of numbers
int sum = numbers.Sum();
double average = numbers.Average();
int min = numbers.Min();
int max = numbers.Max();
int count = numbers.Count();

Console.WriteLine($"Sum: {sum}, Average: {average}, Min: {min}, Max: {max}, Count: {count}");
// Output: Sum: 25, Average: 5, Min: 1, Max: 9, Count: 5

Grouping Data

The GroupBy method in LINQ allows you to group elements in a collection based on a specified key. Here’s an example of grouping a list of strings by their first character:

List<string> names = new List<string> { "Alice", "Bob", "Charlie", "David", "Eve" };

// Group names by their first character using LINQ
var groupedNames = names.GroupBy(name => name[0]);

/* Output:
A: Alice
B: Bob
C: Charlie
D: David
E: Eve
*/
foreach (var group in groupedNames)
{
    Console.Write($"{group.Key}: ");
    foreach (var name in group)
    {
        Console.Write($"{name} ");
    }
    Console.WriteLine();
}

Sorting Data

LINQ provides two sorting methods: OrderBy and OrderByDescending. These methods can be used to sort elements in a collection based on a specified key. You can also use the ThenBy and ThenByDescending methods to sort elements based on multiple keys. Here’s an example:

List<string> names = new List<string> { "Alice", "Bob", "Charlie", "David", "Eve" };

// Sort names by length, then alphabetically using LINQ
var sortedNames = names.OrderBy(name => name.Length).ThenBy(name => name);

// Output: Bob, Eve, Alice, David, Charlie
foreach (var name in sortedNames)
{
    Console.WriteLine(name);
}

Joining Collections

LINQ provides several methods for joining two collections based on matching keys, such as Join, GroupJoin, Union, Intersect, and Except. Here’s an example of using the Join method to join two lists based on a common key:

List<int> ids = new List<int> { 1, 2, 3, 4, 5 };
List<string> names = new List<string> { "Alice", "Bob", "Charlie", "David", "Eve" };

// Join ids and names based on their index
var joinedData = ids.Join(names, id => ids.IndexOf(id), name => names.IndexOf(name), (id, name) => $"{id}: {name}");

// Output: 1: Alice, 2: Bob, 3: Charlie, 4: David, 5: Eve
foreach (var data in joinedData)
{
    Console.WriteLine(data);
}

Conclusion

In this article, we have covered various aspects of using LINQ with collections and data structures in C#. We have gone through the basics of using LINQ with arrays, lists, and dictionaries and explored advanced techniques such as projection, aggregation, grouping, sorting, and joining.

With this knowledge, you can now harness the power of LINQ to effectively manipulate and query your collections in C#. Keep practicing and experimenting with LINQ to become a true master of C# collections!

You May Also Like