« MoreLINQ » : différence entre les versions

De Banane Atomic
Aller à la navigationAller à la recherche
 
(18 versions intermédiaires par le même utilisateur non affichées)
Ligne 1 : Ligne 1 :
[[Category:CSharp]]
[[Category:.NET Application]]
= Links =
= Links =
* [https://github.com/morelinq/MoreLINQ#morelinq README]
* [https://github.com/morelinq/MoreLINQ#morelinq README]
* [https://github.com/morelinq/examples Examples]
* [https://github.com/morelinq/examples Examples]
* [https://morelinq.github.io/3.1/ref/api/html/Methods_T_MoreLinq_MoreEnumerable.htm Documentation]
* [https://morelinq.github.io/3.3/ref/api/html/Methods_T_MoreLinq_MoreEnumerable.htm Documentation]


= Add =
= Add the package to a project =
<kode lang='bash'>
<kode lang='bash'>
dotnet add package MoreLINQ
dotnet add package MoreLINQ
Ligne 13 : Ligne 13 :
== [https://github.com/morelinq/examples/blob/master/m/aggregate.md Aggregate] ==
== [https://github.com/morelinq/examples/blob/master/m/aggregate.md Aggregate] ==


== Batch ==
== [https://morelinq.github.io/3.3/ref/api/html/Overload_MoreLinq_MoreEnumerable_Batch.htm Batch] ==
Batches the source sequence into sized buckets.
Batches the source sequence into sized buckets.
<kode lang='cs'>
<kode lang='cs'>
Ligne 23 : Ligne 23 :
// [7, 8, 9]
// [7, 8, 9]
// [10]
// [10]
</kode>
== [https://morelinq.github.io/3.3/ref/api/html/Overload_MoreLinq_MoreEnumerable_Cartesian.htm 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.
<kode lang='cs'>
new[] { "A", "B" }
    .Cartesian(
        new[] { 1, 2 },
        (x, y) => $"{x}{y}");
// A1
// A2
// B1
// B2
</kode>
== [https://morelinq.github.io/3.3/ref/api/html/Overload_MoreLinq_MoreEnumerable_DistinctBy.htm 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.
<kode lang='cs'>
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);  // 1 2 3
items.DistinctBy(x => x.Name); // 1 2 2
</kode>
== [https://morelinq.github.io/3.3/ref/api/html/Overload_MoreLinq_MoreEnumerable_ExceptBy.htm ExceptBy] ==
Returns the set of elements in the first sequence which aren't in the second sequence, according to a given key selector.
<kode lang='cs'>
var items2 = new[]
{
    new { Id = 2, Name = "One" }
};
items.ExceptBy(items2, x => x.Id);  // 1 3
items.ExceptBy(items2, x => x.Name); // 2 2
</kode>
== [https://morelinq.github.io/3.3/ref/api/html/Overload_MoreLinq_MoreEnumerable_FallbackIfEmpty.htm FallbackIfEmpty] ==
Returns the elements of a sequence and falls back to another if the original sequence is empty.
<kode lang='cs'>
var integers = Array.Empty<int>();
integers.FallbackIfEmpty(new[] { 1, 2 });
integers.FallbackIfEmpty(1);    // int[] { 1 }
integers.FallbackIfEmpty(1, 2); // int[] { 1, 2 }
</kode>
== Left / Right / Full join ==
<kode lang='cs'>
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();
// (1, 1) (2, 2) (3 ,0)
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();
// (1, 1) (2, 2) (3 ,0) (0, 4)
</kode>
* [https://morelinq.github.io/3.3/ref/api/html/Overload_MoreLinq_MoreEnumerable_LeftJoin.htm Left join]
* [https://morelinq.github.io/3.3/ref/api/html/Overload_MoreLinq_MoreEnumerable_FullJoin.htm Full join]
= [https://stackoverflow.com/questions/8474882/ambiguous-extension-method Ambiguous call between Linq.Enumerable.ToHashSet and MoreLinq.MoreEnumerable.ToHashSet] =
Workaround: move the MoreLinq using statement inside the namespace.
<kode lang='cs'>
// using MoreLinq;
namespace MyNamespace;
using MoreLinq;
</kode>
</kode>

Dernière version du 19 août 2023 à 11:37

Links

Add the package to a project

Bash.svg
dotnet add package MoreLINQ

Operators

Aggregate

Batch

Batches the source sequence into sized buckets.

Cs.svg
Enumerable
    .Range(1, 10)
    .Batch(3);  // split the sequence into sequences of max length 3
// [1, 2, 3]
// [4, 5, 6]
// [7, 8, 9]
// [10]

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.

Cs.svg
new[] { "A", "B" }
    .Cartesian(
        new[] { 1, 2 },
        (x, y) => $"{x}{y}");
// A1
// A2
// B1
// B2

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.

Cs.svg
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);   // 1 2 3
items.DistinctBy(x => x.Name); // 1 2 2

ExceptBy

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

Cs.svg
var items2 = new[]
{
    new { Id = 2, Name = "One" }
};

items.ExceptBy(items2, x => x.Id);   // 1 3
items.ExceptBy(items2, x => x.Name); // 2 2

FallbackIfEmpty

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

Cs.svg
var integers = Array.Empty<int>();

integers.FallbackIfEmpty(new[] { 1, 2 });
integers.FallbackIfEmpty(1);    // int[] { 1 }
integers.FallbackIfEmpty(1, 2); // int[] { 1, 2 }

Left / Right / Full join

Cs.svg
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();

// (1, 1) (2, 2) (3 ,0)

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();

// (1, 1) (2, 2) (3 ,0) (0, 4)

Ambiguous call between Linq.Enumerable.ToHashSet and MoreLinq.MoreEnumerable.ToHashSet

Workaround: move the MoreLinq using statement inside the namespace.

Cs.svg
// using MoreLinq;

namespace MyNamespace;

using MoreLinq;