LINQ (Language Integrated Query) is a powerful feature in C# that allows developers to query data from various data sources such as arrays, lists, and databases. While many developers are familiar with the basic LINQ functions such as Where, Select, and OrderBy, there are several lesser-used LINQ functions that can be very useful in certain scenarios. In this blog post, we will explore some of these lesser-used LINQ functions and provide code samples to demonstrate their usage.
TakeWhile and SkipWhile
The TakeWhile and SkipWhile functions are used to take or skip elements from a sequence based on a condition. The TakeWhile function takes elements from a sequence while a condition is true, and stops taking elements as soon as the condition becomes false. The SkipWhile function skips elements from a sequence while a condition is true, and starts taking elements as soon as the condition becomes false.
var numbers = new List<int> { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
var takeWhile = numbers.TakeWhile(n => n < 5); // returns 1, 2, 3, 4
var skipWhile = numbers.SkipWhile(n => n < 5); // returns 5, 6, 7, 8, 9, 10
GroupBy
The GroupBy function is used to group elements in a sequence based on a key. The key is determined by a function that is passed to the GroupBy function. The result is a sequence of groups, where each group contains a key and a sequence of elements that have the same key.
var words = new List<string> { "apple", "banana", "cherry", "date", "elderberry" };
var groups = words.GroupBy(w => w[0]); // groups words by their first letter
foreach (var group in groups)
{
Console.WriteLine($"Words that start with {group.Key}:");
foreach (var word in group)
{
Console.WriteLine($" {word}");
}
}
Output:
Words that start with a:
apple
Words that start with b:
banana
Words that start with c:
cherry
Words that start with d:
date
Words that start with e:
elderberry
Zip
The Zip function is used to combine two sequences into a single sequence of pairs. The resulting sequence contains pairs of elements from the two input sequences, where the first element in each pair comes from the first sequence, and the second element comes from the second sequence.
var numbers1 = new List<int> { 1, 2, 3 };
var numbers2 = new List<int> { 4, 5, 6 };
var pairs = numbers1.Zip(numbers2, (n1, n2) => (n1, n2)); // returns (1, 4), (2, 5), (3, 6)
Aggregate
The Aggregate function is used to perform a custom aggregation operation on a sequence. The operation is defined by a function that takes two elements from the sequence and returns a single result. The result is then used as the first parameter for the next call to the function, along with the next element in the sequence.
var numbers = new List<int> { 1, 2, 3, 4, 5 };
var product = numbers.Aggregate((a, b) => a * b); // returns 120 (1 * 2 * 3 * 4 * 5)
Conclusion
In this blog post, we explored some of the lesser-used LINQ functions in C#. While these functions may not be used as frequently as some of the more common LINQ functions, they can be very useful in certain scenarios. By understanding these functions and their usage, developers can write more efficient and concise code when working with sequences of data.


Leave a comment