Skip to content
Snippets Groups Projects
README.md 20.84 KiB

MoreLINQ

LINQ to Objects is missing a few desirable features.

This project enhances LINQ to Objects with extra methods, in a manner which keeps to the spirit of LINQ.

MoreLINQ is available for download and installation as NuGet packages.

Documentation for the stable and beta releases can be found at morelinq.github.io.

Usage

MoreLINQ can be used in one of two ways. The simplest is to just import the MoreLinq namespace and all extension methods become instantly available for you to use on the types they extend (typically some instantiation of IEnumerable<T>). In some very rare instances, however, doing so can cause conflicts with other libraries you may be using that incidentally also extend the same type with an identically named method and signature. This happened with MoreLINQ, for example, when Microsoft .NET Framework 4.0 introduced Zip and MoreLINQ already had one. Starting with version 3.0 of MoreLINQ, you can reduce the potential for present (or even future) conflicts by individually importing just the extension methods you need using the static imports feature introduced in C# 6:

using static MoreLinq.Extensions.LagExtension;
using static MoreLinq.Extensions.LeadExtension;

In the example above, only the Lag and Lead extension methods will be available in scope.

Apart from extension methods, MoreLINQ also offers regular static method that generate (instead of operating on) sequences, like Unfold, Random, Sequence and others. If you want to use these while statically importing other individual extension methods, you can do so via aliasing:

using static MoreLinq.Extensions.LagExtension;
using static MoreLinq.Extensions.LeadExtension;
using MoreEnumerable = MoreLinq.MoreEnumerable;

In the example above, Lag and Lead will be available as extension methods as well as all the regular static methods on MoreEnumerable but without any of the extension methods offered by MoreEnumerable.

Building

Run either build.cmd if building on Windows or build.sh if building on macOS or a Linux distribution supported by .NET.

Some code in the project is generated using T4 templates. To regenerate the code from modified templates, run MoreLinq\tt.cmd (Windows) or MoreLinq/tt.sh depending on your platform.

Building the documentation is supported on Windows only and requires Sandcastle Help File Builder (SHFB). Executing builddocs.cmd generates the documentation in the docs/api directory. It can be browsed locally using any HTTP server of static files, like dotnet-serve.

Operators

Acquire

Ensures that a source sequence of disposable objects are all acquired successfully. If the acquisition of any one fails then those successfully acquired till that point are disposed.

Aggregate

Applies multiple accumulators sequentially in a single pass over a sequence.

This method has 7 overloads.

AggregateRight

Applies a right-associative accumulator function over a sequence. This operator is the right-associative version of the Aggregate LINQ operator.

This method has 3 overloads.

Append

Returns a sequence consisting of the head element and the given tail elements.

Assert

Asserts that all elements of a sequence meet a given condition otherwise throws an exception.

This method has 2 overloads.

AssertCount

Asserts that a source sequence contains a given count of elements.

This method has 2 overloads.

AtLeast

Determines whether or not the number of elements in the sequence is greater than or equal to the given integer.

AtMost

Determines whether or not the number of elements in the sequence is lesser than or equal to the given integer.

Backsert

Inserts the elements of a sequence into another sequence at a specified index from the tail of the sequence, where zero always represents the last position, one represents the second-last element, two represents the third-last element and so on.

Batch

Batches the source sequence into sized buckets.

This method has 4 overloads, 2 of which are experimental.

Cartesian

Returns the Cartesian product of two or more sequences by combining each element from the sequences and applying a user-defined projection to the set.

This method has 7 overloads.

Choose

Applies a function to each element of the source sequence and returns a new sequence of result elements for source elements where the function returns a couple (2-tuple) having a true as its first element and result as the second.

CompareCount

Compares two sequences and returns an integer that indicates whether the first sequence has fewer, the same or more elements than the second sequence.

Concat

Returns a sequence consisting of the head element and the given tail elements.

This method is obsolete and will be removed in a future version. Use Append instead.

Consume

Completely consumes the given sequence. This method uses immediate execution, and doesn't store any data during execution

CountBetween

Determines whether or not the number of elements in the sequence is between an inclusive range of minimum and maximum integers.

CountBy

Applies a key-generating function to each element of a sequence and returns a sequence of unique keys and their number of occurrences in the original sequence.

This method has 2 overloads.

CountDown

Provides a countdown counter for a given count of elements at the tail of the sequence where zero always represents the last element, one represents the second-last element, two represents the third-last element and so on.

DistinctBy

Returns all distinct elements of the given source, where "distinctness" is determined via a projection and the default equality comparer for the projected type.

This method has 2 overloads.

EndsWith

Determines whether the end of the first sequence is equivalent to the second sequence.

This method has 2 overloads.

EquiZip

Returns a projection of tuples, where each tuple contains the N-th element from each of the argument sequences. An exception is thrown if the input sequences are of different lengths.

This method has 3 overloads.

Exactly

Determines whether or not the number of elements in the sequence is equals to the given integer.

ExceptBy

Returns the set of elements in the first sequence which aren't in the second sequence, according to a given key selector.

This method has 2 overloads.

Exclude

Excludes elements from a sequence starting at a given index

FallbackIfEmpty

Returns the elements of a sequence and falls back to another if the original sequence is empty.

This method has 6 overloads.

FillBackward

Returns a sequence with each null reference or value in the source replaced with the following non-null reference or value in that sequence.

This method has 3 overloads.

FillForward

Returns a sequence with each null reference or value in the source replaced with the previous non-null reference or value seen in that sequence.

This method has 3 overloads.

Flatten

Flattens a sequence containing arbitrarily-nested sequences.

This method has 3 overloads.

Fold

Returns the result of applying a function to a sequence with 1 to 16 elements.

This method has 16 overloads.

ForEach

Immediately executes the given action on each element in the source sequence.

This method has 2 overloads.

From

Returns a sequence containing the values resulting from invoking (in order) each function in the source sequence of functions.

This method has 4 overloads.

FullGroupJoin

Performs a Full Group Join between the and sequences.

This method has 4 overloads.

FullJoin

Performs a full outer join between two sequences.

This method has 4 overloads.

Generate

Returns a sequence of values consecutively generated by a generator function

GenerateByIndex

Returns a sequence of values based on indexes

GroupAdjacent

Groups the adjacent elements of a sequence according to a specified key selector function.

This method has 6 overloads.

Incremental

Incremental was redundant with Pairwise and so deprecated since version 2.1. It was eventually removed in version 3.0.

Index

Returns a sequence of where the key is the zero-based index of the value in the source sequence.

This method has 2 overloads.

IndexBy

Applies a key-generating function to each element of a sequence and returns a sequence that contains the elements of the original sequence as well its key and index inside the group of its key. An additional argument specifies a comparer to use for testing equivalence of keys.

This method has 2 overloads.

Insert

Inserts the elements of a sequence into another sequence at a specified index.

Interleave

Interleaves the elements of two or more sequences into a single sequence, skipping sequences as they are consumed.

Lag

Produces a projection of a sequence by evaluating pairs of elements separated by a negative offset.

This method has 2 overloads.

Lead

Produces a projection of a sequence by evaluating pairs of elements separated by a positive offset.

This method has 2 overloads.

LeftJoin