This library adds Additional LINQ style Deferred execution and Immediate enumeration mode extension methods for .NET.
Now updated to use C# 14 and extension everything as of version 0.12.0. C# language version must be set to 14 or higher for EnhancedLinq to work.
For a comprehensive list of included methods, check out the following resources:
EnhancedLinq comes with several packages tailored to your needs:
EnhancedLinq: The core package that enhances your LINQ experience.EnhancedLinq.Memory: This package is specifically forSpan<T>andMemory<T>, providing helpful immediate mode extensions.EnhancedLinq.MsExtensions: Focused onStringSegmentfromMicrosoft.Extensions.Primitives, with plans to potentially expand support for other Microsoft.Extensions packages.
Getting started with EnhancedLinq is easy! You can install the packages using the .NET SDK CLI, your IDE's package manager, or directly from the NuGet website.
| Package Id | NuGet Link | .NET SDK CLI Command |
|---|---|---|
| EnhancedLinq | EnhancedLinq NuGet | dotnet add package EnhancedLinq. |
| EnhancedLinq.Memory | EnhancedLinq.Memory NuGet | dotnet add package EnhancedLinq.Memory |
| EnhancedLinq.MsExtensions | EnhancedLinq.MsExtensions NuGet | dotnet add package EnhancedLinq.MsExtensions |
Here are some examples demonstrating how to use some methods provided by EnhancedLinq.
ElementsAt This example shows how to find the elements at a given sequence of indices.
var fruits = ["Apple", "Banana", "Cherry", "Date", "Elderberry" ];
var indices = [1, 3]; // We want to retrieve "Banana" and "Date"
IEnumerable<string> selectedFruits = fruits.ElementsAt(indices);
Console.WriteLine("Selected Fruits:");
foreach (string fruit in selectedFruits)
{
Console.WriteLine(fruit); // Outputs: Banana, Date
}IndicesOf This example shows how to find all the indices of a specific element in an IEnumerable.
var numbers = [ 1, 2, 3, 2, 4, 2, 5 ];
int target = 2; // We want to find the indices of the number 2
IEnumerable<int> indices = numbers.IndicesOf(target);
Console.WriteLine("Indices of target element:");
foreach (var index in indices)
{
Console.WriteLine(index); // Outputs: 1, 3, 5
}ContainsDuplicates This example shows how to check if an IEnumerable contains any duplicate elements.
var fruits = ["Apple", "Banana", "Cherry", "Apple" ]; // Contains a duplicate
bool hasDuplicates = fruits.ContainsDuplicates();
Console.WriteLine($"Does the list contain duplicates? {hasDuplicates}"); // Output: TrueIndexOf This example shows how to find the index of the first element that matches a predicate in an IEnumerable.
var numbers = new List<int> { 10, 20, 30, 40, 50 };
// Define a predicate to find the first number greater than 25
Func<int, bool> predicate = n => n > 25;
int index = numbers.IndexOf(predicate);
Console.WriteLine($"Zero based index of the first element greater than 25: {index}"); // Output: 2To build EnhancedLinq from source, follow these steps:
- Clone the repository.
- Open the solution in your preferred IDE or Code Editor.
- Build the desired project to restore dependencies and compile the project.
I welcome contributions. If you have ideas for new features, improvements, or bug fixes, please check out the contributing guidelines for more information.
EnhancedLinq is licensed under the MPL 2.0 license. Feel free to use and modify EnhancedLinq according to the terms of the license.
If you have any questions or experience any issues, please open a discussion in the repository's GitHub issues page.
While EnhancedLinq is a powerful tool, you may wish to explore these alternatives:
In no particular order, these are some of the reasons why EnhancedLinq was created:
- To move DotExtension LINQ style methods/operations to a separate library.
- Hardly any .NET libraries exist that bring Linq style methods/operations to Span/Memory.
- Hardly any .NET libraries exist that bring Linq style methods/operations to StringSegment and other
Microsoft.Extensions.Primitivespackage types.