📜 ⬆️ ⬇️

Library morelinq: what is missing in LINQ to Objects out of the box

I think many readers of the .Net blog know the name Jon Skeet . Especially after yesterday 's post of user SergeyT . Therefore, I will not repeat the comparison with Chuck Norris and the first place in karma on StackOverflow.com. But mentioning once again about his wonderful book “C # In Depth” will definitely not be superfluous. LINQ in general and LINQ to Objects in particular are central to it. John very thoroughly describes all the features of the C # language and the .Net platform, which made possible the emergence of LINQ 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 some highly needed 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 .

Morelinq library operators


BatchTurns one sequence into several sequences of n elements.
ConcatAttaches an item to a collection or a collection to an item.
Consume"Absorbs" the collection, without producing any action on the elements.
DistinctByReturns only unique elements (by specified criteria).
EquizipCreates a new sequence, where each element is created based on the corresponding elements of the original sequences. If sequences have different numbers of elements, an InvalidOperationException will be thrown.
ExceptByReturns the elements of the first sequence that are not contained in the second (by the given criterion).
ForEachPerforms an action on each element of the sequence.
GenerateGenerates sequences on the initial element and the function generator.
GenerateByIndexGenerates a sequence of item indices.
GroupadjacentSimilar to 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 a given criterion.
MinbyReturns the minimum element of a sequence according to a given criterion.
PadIf the number of elements in the sequence is less than the specified number, the sequence complements the default values ​​to the specified number.
PairwiseReturns a sequence of function results for the current and previous elements (does not apply to the first element).
PipeReturns the original sequence by performing an Action on each element.
PrependComplements the beginning of the collection with a specified element.
PreScanReturns the sequence of the original length, in which the Nth element is determined by applying the specified transform to N-1 elements.
ScanReturns the sequence of the original length, in which the Nth element is determined by applying the specified transform to N elements.
SingleOrFallbackReturns the only element of a sequence, or the result of the specified delegate if the sequence is empty.
SkipUntilSkips the elements of the original sequence until the specified condition is true. The current item will be the last skipped.
SplitSplits a sequence with a specified delimiter (returns a sequence of sequences).
TakeEveryReturns every Nth element of the source sequence.
TakeLastReturns the last N elements of the source sequence.
TakeUntilReturns the elements of the original sequence until the specified condition becomes true. The current item will be the last returned item.
TodatatableAllows you to convert a sequence to a new DataTable or fill the existing one. It is possible to set the lamdas to get the values ​​for the table fields from the source element.
ToDelimitedStringConverts a sequence to a delimited string (something that usually has to be done through a tedious Aggregate).
TohashsetReturns HashSet 〈T & kang; from the original elements.
ZipSame as EquiZip, but the length of the resulting sequence will be equal to the length of the smallest of the original.
ZipLongestSame 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 missing values).

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

Together with the source code of the library there is excellent documentation in the MSDN style, in which all operators are described in detail, their parameters and examples of use are available. There are also comments in the source code.
')
In conclusion, I would like to draw readers' attention to the two other libraries of authorship by John Skit - MiscUtils and NodaTime ( NuGet Package ). The latter is particularly interesting - the library is designed to work with date / time. John has been doing it for the last few years and finally released version 1.0 in November last year. In his blog, you can read a lot of interesting things about what the standard .Net classes are bad for these purposes and what underwater rakes await a developer who is seriously working with time.

UPD: Translation of an article about NodaTime .

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

Source: https://habr.com/ru/post/167831/


All Articles