LINQ (Language Integrated Query) is a powerful feature in .NET that allows developers to query collections in a concise and readable manner. While methods like Where, Select, OrderBy, and GroupBy are widely used and appreciated, there are several other LINQ methods that are often underappreciated but can be incredibly useful.
In this blog post, we'll explore the top 10 underappreciated LINQ methods with examples to help you harness their full potential.
1. Aggregate
The Aggregate method applies an accumulator function over a sequence. This is useful for performing complex calculations or aggregations.
using System;
using System.Linq;
class Program
{
static void Main()
{
var numbers = new[] { 1, 2, 3, 4, 5 };
var sum = numbers.Aggregate((acc, x) => acc + x);
Console.WriteLine($"Sum: {sum}");
var product = numbers.Aggregate((acc, x) => acc * x);
Console.WriteLine($"Product: {product}");
}
}
2. Zip
The Zip method merges two sequences by combining their elements pairwise using a specified function.
using System;
using System.Linq;
class Program
{
static void Main()
{
var numbers = new[] { 1, 2, 3 };
var words = new[] { "one", "two", "three" };
var zipped = numbers.Zip(words, (n, w) => $"{n} is {w}");
foreach (var item in zipped)
{
Console.WriteLine(item);
}
}
}
3. Chunk
The Chunk method splits a sequence into chunks of a specified size.
using System;
using System.Linq;
class Program
{
static void Main()
{
var numbers = Enumerable.Range(1, 10);
var chunks = numbers.Chunk(3);
foreach (var chunk in chunks)
{
Console.WriteLine(string.Join(", ", chunk));
}
}
}
4. TakeWhile and SkipWhile
The TakeWhile method returns elements from a sequence as long as a specified condition is true. The SkipWhile method skips elements as long as the condition is true and then returns the remaining elements.
using System;
using System.Linq;
class Program
{
static void Main()
{
var numbers = new[] { 1, 2, 3, 4, 5, 6 };
var takeWhileLessThanFour = numbers.TakeWhile(n => n < 4);
Console.WriteLine("TakeWhile < 4: " + string.Join(", ", takeWhileLessThanFour));
var skipWhileLessThanFour = numbers.SkipWhile(n => n < 4);
Console.WriteLine("SkipWhile < 4: " + string.Join(", ", skipWhileLessThanFour));
}
}
5. DistinctBy
The DistinctBy method returns distinct elements from a sequence based on a specified key selector.
using System;
using System.Collections.Generic;
using System.Linq;
class Program
{
static void Main()
{
var people = new List<Person>
{
new Person { Name = "Alice", Age = 30 },
new Person { Name = "Bob", Age = 40 },
new Person { Name = "Alice", Age = 35 },
};
var distinctNames = people.DistinctBy(p => p.Name);
foreach (var person in distinctNames)
{
Console.WriteLine(person.Name);
}
}
class Person
{
public string Name { get; set; }
public int Age { get; set; }
}
}

Join the conversation! Your thoughts help the community grow.