Back to Home

Morelinq library: what's missing in LINQ to Objects out of the box

.net · morelinq · LINQ · John Skeet · bike

Morelinq library: what's missing in LINQ to Objects out of the box

    I think many readers of the .Net blog know the name Jon Skeet . Especially after last year's post of the user SergeyT . Therefore, I will not repeat the comparison with Chuck Norris and the first place in karma on StackOverflow.com. But to mention once again about his wonderful book “C # In Depth”certainly not superfluous. Central to it is LINQ in general, and LINQ to Objects in particular. John describes in great detail all the features of the C # language and the .Net platform that made LINQ possible in its current form, as well as the details of its implementation. It was after reading this book that I began to actively use LINQ to Objects in my projects. However, the standard library lacks a few critical operators. Fortunately, John Skeet corrected this misunderstanding. So there was a small but very useful library morelinq . And since the end of last year, it is available as a NuGet package .

    Library operators morelinq


    BatchTurns one sequence into several sequences of n elements.
    ConcatAttaches an item to a collection or collection to an item.
    Consume“Absorbs” the collection without performing any actions on the elements.
    DistinctbyReturns only unique elements (according to the specified criteria).
    EquizipCreates a new sequence, where each element is created based on the corresponding elements of the source sequences. If sequences have a different number of elements, an InvalidOperationException will be thrown.
    ExceptbyReturns the elements of the first sequence that are not contained in the second (by the specified criterion).
    ForeachPerforms an action on each element of a sequence.
    GenerateGenerates sequences from the start element and the generator function.
    GenerateByIndexGenerates a sequence by element indices.
    GroupadjacentLike GroupBy, but only consecutive items fall into the group.
    IndexReturns a sequence of index-value pairs.
    MaxbyReturns the maximum element of a sequence according to the specified criterion.
    MinbyReturns the minimum element of a sequence according to the specified criteria.
    PadIf the number of elements in the sequence is less than the specified value, supplements the sequence with default values ​​to the specified number.
    PairwiseReturns the sequence of results of the function of the current and previous element (does not apply to the first element).
    PipeReturns the original sequence by performing an Action on each element.
    Prepare Complement the beginning of the collection with the specified element.
    PreScanReturns a sequence of the original length in which the Nth element is determined by applying the specified transformation to N-1 elements.
    ScanReturns a sequence of the original length in which the Nth element is determined by applying the specified transformation to N elements.
    SingleOrFallbackReturns a single element of a sequence or the result of a given delegate if the sequence is empty.
    SkipuntilSkips the elements of the original sequence until the specified condition becomes true. The current item will be the last skipped.
    SplitSeparates a sequence with the specified delimiter (returns a sequence of sequences).
    TakeEveryReturns every Nth element of the original sequence.
    TakelastReturns the last N elements of the original sequence.
    TakeUntilReturns the elements of the original sequence until the specified condition becomes true. The current item will be the last returned.
    ToDataTableAllows you to convert a sequence to a new DataTable or fill an existing one. It is possible to set the values ​​for the table fields from the source element by llamas.
    ToDelimitedStringConverts a sequence to a delimited string (what you usually have to do through a boring Aggregate).
    TohashsetReturns HashSet 〈T & kang; from the source elements.
    ZipThe same as EquiZip, but the length of the resulting sequence will be equal to the length of the smallest of the original.
    ZiplongestThe same as EquiZip, but the length of the resulting sequence will be equal to the length of the largest of the original ones (the default value will be used as the missing values).

    Most operators are overloaded for greater flexibility of use (for example, you can set your own IComparer, etc.). In addition to these operators, there are two more debugging options - AssertCount (check the number of elements in the sequence) and Trace (displays all elements in the debug console).

    Along with the source code of the library is an excellent documentation in the style of MSDN, which describes in detail all the operators, their parameters and examples of use. There are also comments in the source code.

    In conclusion, I want to draw the attention of readers to two other libraries of authorship by John Skeet - MiscUtils and NodaTime ( NuGet Package) The latter is especially interesting - the library is designed to work with date / time. John has been practicing it for the past few years and in November last year finally released version 1.0. In his blog, you can read a lot of interesting things on the topic of why the standard .Net classes are bad for these purposes and what kind of underwater rake awaits a developer who works seriously over time.

    UPD: Translation of an article about NodaTime .

    UPD2: I did not use the word “projection” in the article , because in my opinion it is not used in the Russian-speaking community. I would like to see your opinion in the survey and comments.

    Only registered users can participate in the survey. Please come in.

    Would the word “projection” be understood in the description of the morelinq operators? (e.g., DistinctBy is described in the documentation as "Returns all distinct elements of the given source, where 'distinctness' is determined via a projection and the default eqaulity comparer for the projected type."

    • 68.4% Yes 52
    • 31.5% No 24

    Read Next