✨ Shield now has support for Avalonia UI

LINQ Extension Methods in C#: What you need to know

Apr 8, 2023 | .NET, C#

In this article, we’ll dive deep into the world of LINQ extension methods in C#. We’ll learn how to create custom LINQ methods, and explore various ways to extend LINQ capabilities. Let’s get started!

Introduction to LINQ Extension Methods

LINQ (Language Integrated Query) is a powerful feature in C# that allows developers to write queries against collections and data sources using a consistent and expressive syntax. One of the key aspects of LINQ is its extensibility, which is made possible through extension methods.

In the following sections, we’ll cover the basics of extension methods, and how to create custom LINQ extension methods to enhance our querying capabilities.

What are Extension Methods?

Extension methods are a C# feature that allows developers to “add” new methods to existing types without modifying the original type’s code or creating a derived type. They are particularly useful when working with LINQ, as we can create custom query operators that seamlessly integrate with the existing LINQ syntax.

An extension method is defined as a static method within a static class, with the first parameter of the method having the this modifier followed by the type being extended. Here’s an example of a simple extension method:

public static class StringExtensions
{
    public static string Reverse(this string input)
    {
        char[] chars = input.ToCharArray();
        Array.Reverse(chars);
        return new string(chars);
    }
}

In this example, we’ve created an extension method called Reverse that works on string objects. To use this extension method, we simply call it as if it were an instance method of the string class:

string name = "LINQ";
string reversedName = name.Reverse(); // Output: "QNIL"

Creating Custom LINQ Extension Methods

Now that we understand the basics of extension methods, let’s see how we can create custom LINQ extension methods. We’ll start by creating a simple extension method that filters a collection of integers based on whether they are even or odd.

public static class LinqExtensions
{
    public static IEnumerable<int> WhereEven(this IEnumerable<int> source)
    {
        foreach (int number in source)
        {
            if (number % 2 == 0)
            {
                yield return number;
            }
        }
    }
}

This custom LINQ extension method can be used just like any other LINQ method:

List<int> numbers = new List<int> { 1, 2, 3, 4, 5, 6 };
IEnumerable<int> evenNumbers = numbers.WhereEven(); // Output: { 2, 4, 6 }

Advanced Usage of LINQ Extension Methods

In this section, we’ll explore some advanced techniques for creating LINQ extension methods that provide more flexibility and power to our queries.

Using Lambda Expressions and Func Delegates

To create more versatile extension methods, we can use lambda expressions and Func delegates as parameters. This allows us to pass custom logic to our extension method, making it more reusable and adaptable to different scenarios. Here’s an example of a custom Where method that accepts a Func delegate as a parameter:

public static IEnumerable<T> CustomWhere<T>(this IEnumerable<T> source, Func<T, bool> predicate)
{
    foreach (T element in source)
    {
        if (predicate(element))
        {
            yield return element;
        }
    }
}

With this custom Where method, we can now pass in any filtering logic as a lambda expression:

List<int> numbers = new List<int> { 1, 2, 3, 4, 5, 6 };
IEnumerable<int> evenNumbers = numbers.CustomWhere(n => n % 2 == 0); // Output: { 2, 4, 6 }

Combining Extension Methods

One of the strengths of LINQ is the ability to chain multiple query operators together to form more complex queries. This is also true for custom LINQ extension methods. Let’s create another custom extension method that retrieves every nth element from a collection:

public static IEnumerable<T> EveryNth<T>(this IEnumerable<T> source, int n)
{
    int i = 0;
    foreach (T element in source)
    {
        if (i % n == 0)
        {
            yield return element;
        }
        i++;
    }
}

Now, we can chain our custom WhereEven and EveryNth methods together to create more complex queries:

List<int> numbers = new List<int> { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
IEnumerable<int> result = numbers.WhereEven().EveryNth(2); // Output: { 2, 6, 10 }

Advanced Techniques with LINQ Extension Methods

In this section, we’ll dive deeper into some advanced techniques for creating powerful and flexible LINQ extension methods.

Aggregating Data with Custom Extension Methods

Aggregating data is a common operation in LINQ queries. Let’s create a custom extension method that calculates the median of a collection of numbers:

public static double Median(this IEnumerable<double> source)
{
    if (!source.Any())
    {
        throw new InvalidOperationException("The source sequence is empty.");
    }

    var sortedNumbers = source.OrderBy(x => x).ToList();
    int count = sortedNumbers.Count;
    if (count % 2 == 0)
    {
        return (sortedNumbers[count / 2 - 1] + sortedNumbers[count / 2]) / 2.0;
    }
    else
    {
        return sortedNumbers[count / 2];
    }
}

Now, we can use the Median extension method to calculate the median of a collection of numbers:

List<double> numbers = new List<double> { 1.0, 2.0, 3.0, 4.0, 5.0 };
double median = numbers.Median(); // Output: 3.0

Creating Extension Methods with Multiple Input Sequences

In some cases, we might want to create LINQ extension methods that operate on multiple input sequences. For example, let’s create a custom Zip method that combines two sequences:

public static IEnumerable<TResult> CustomZip<TFirst, TSecond, TResult>(
    this IEnumerable<TFirst> first,
    IEnumerable<TSecond> second,
    Func<TFirst, TSecond, TResult> resultSelector)
{
    using (var firstEnumerator = first.GetEnumerator())
    using (var secondEnumerator = second.GetEnumerator())
    {
        while (firstEnumerator.MoveNext() && secondEnumerator.MoveNext())
        {
            yield return resultSelector(firstEnumerator.Current, secondEnumerator.Current);
        }
    }
}

Now, we can use the CustomZip extension method to combine two sequences using a custom resultSelector function:

List<int> firstList = new List<int> { 1, 2, 3 };
List<int> secondList = new List<int> { 4, 5, 6 };

IEnumerable<int> result = firstList.CustomZip(secondList, (a, b) => a * b); // Output: { 4, 10, 18 }

Working with Expression Trees

Expression trees are a powerful feature in C# that allows us to represent code as data structures that can be analyzed and modified at runtime. By using expression trees in our LINQ extension methods, we can build more flexible and dynamic queries.

Let’s create a custom OrderBy extension method that accepts a string representing the property to sort by:

public static IOrderedEnumerable<TSource> CustomOrderBy<TSource>(
    this IEnumerable<TSource> source,
    string propertyName)
{
    // Create an Expression tree representing the property to sort by
    var parameter = Expression.Parameter(typeof(TSource), "x");
    var property = Expression.Property(parameter, propertyName);
    var lambda = Expression.Lambda<Func<TSource, object>>(property, parameter);

    // Use reflection to invoke the built-in OrderBy method with the generated lambda
    var orderByMethod = typeof(Enumerable).GetMethods()
        .First(m => m.Name == "OrderBy" && m.GetParameters().Length == 2)
        .MakeGenericMethod(typeof(TSource), typeof(object));

    return (IOrderedEnumerable<TSource>)orderByMethod.Invoke(null, new object[] { source, lambda.Compile() });
}

With this custom OrderBy method, we can now sort a collection of objects based on a property name specified as a string:

public class Person
{
    public string Name { get; set; }
    public int Age { get; set; }
}

List<Person> people = new List<Person>
{
    new Person { Name = "Alice", Age = 30 },
    new Person { Name = "Bob", Age = 25 },
    new Person { Name = "Charlie", Age = 35 }
};

IOrderedEnumerable<Person> sortedPeople = people.CustomOrderBy("Age"); // Output: { Bob, Alice, Charlie }

Implementing Paging with LINQ Extension Methods

Paging is a common requirement in many applications when dealing with large datasets. Let’s create a custom extension method that implements paging for a collection of items:

public static IEnumerable<T> Page<T>(this IEnumerable<T> source, int pageIndex, int pageSize)
{
    if (pageIndex < 0)
    {
        throw new ArgumentOutOfRangeException(nameof(pageIndex), "Page index must be greater than or equal to zero.");
    }

    if (pageSize <= 0)
    {
        throw new ArgumentOutOfRangeException(nameof(pageSize), "Page size must be greater than zero.");
    }

    return source.Skip(pageIndex * pageSize).Take(pageSize);
}

Now, we can easily paginate a collection of items using the custom Page extension method:

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

IEnumerable<int> firstPage = numbers.Page(0, 3); // Output: { 1, 2, 3 }
IEnumerable<int> secondPage = numbers.Page(1, 3); // Output: { 4, 5, 6 }

Conclusion

In this article, we explored the concept of LINQ extension methods in C#, and how they can be used to create custom query operators that extend LINQ’s capabilities.

We learned about the basics of extension methods, how to create custom LINQ extension methods, and some advanced techniques for creating more versatile and powerful queries. By leveraging LINQ extension methods, we can make our code more expressive, maintainable, and reusable. Happy coding!

You May Also Like