Links
Add the package to a project
|
dotnet add package MoreLINQ
|
Operators
Batches the source sequence into sized buckets.
|
Enumerable
.Range(1, 10)
.Batch(3);
|
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.
|
new[] { "A", "B" }
.Cartesian(
new[] { 1, 2 },
(x, y) => $"{x}{y}");
|
Returns all distinct elements of the given source, where "distinctness" is determined via a projection and the default equality comparer for the projected type.
|
var items = new[]
{
new { Id = 1, Name = "One" },
new { Id = 2, Name = "Two" },
new { Id = 3, Name = "One" },
new { Id = 2, Name = "Two again" }
};
items.DistinctBy(x => x.Id);
items.DistinctBy(x => x.Name);
|
Returns the set of elements in the first sequence which aren't in the second sequence, according to a given key selector.
|
var items2 = new[]
{
new { Id = 2, Name = "One" }
};
items.ExceptBy(items2, x => x.Id);
items.ExceptBy(items2, x => x.Name);
|
Returns the elements of a sequence and falls back to another if the original sequence is empty.
|
var integers = Array.Empty<int>();
integers.FallbackIfEmpty(new[] { 1, 2 });
integers.FallbackIfEmpty(1);
integers.FallbackIfEmpty(1, 2);
|
Left / Right / Full join
|
var a1 = new[] {1, 2, 3};
var a2 = new[] {1, 2, 4};
var lj = a1.LeftJoin(
a2,
x => x, // a1 key selector
x => x, // a2 key selector
x => (x, 0), // selector when a1 element doesn't match a2 element
(x, y) => (x, y)) // match selector
.ToArray();
var fj = a1.FullJoin(
a2,
x => x, // a1 key selector
x => x, // a2 key selector
x => (x, 0), // selector when a1 element doesn't match a2 element
x => (0, x), // selector when a2 element doesn't match a1 element
(x, y) => (x, y)) // match selector
.ToArray();
|
Workaround: move the MoreLinq using statement inside the namespace.
|
namespace MyNamespace;
using MoreLinq;
|