With extension methods, we can write handy LINQ operators which solve generic problems.
I want to hear which methods or overloads you are missing in the System.Linq
namespace and how you implemented them.
Clean and elegant implementations, maybe using existing methods, are preferred.
IndexOf
Coalesce
Loop
Here's a kinda cool one I just thought of. (If I just thought of it, maybe it's not that useful? But I thought of it because I have a use for it.) Loop through a sequence repeatedly to generate an infinite sequence. This accomplishes something kind of like what
Enumerable.Range
andEnumerable.Repeat
give you, except it can be used for an arbitrary (unlikeRange
) sequence (unlikeRepeat
):Usage:
Output:
Note: I suppose you could also accomplish this with something like:
...but I think
Loop
is clearer.MinElement
Min
only returns the minimum value returned by the specified expression, but not the original element that gave this minimum element.WhereIf
Optional
Where
clause onIEnumerable
andIQueryable
. Avoids if statements when building predicates & lambdas for a query. Useful when you don't know at compile time whether a filter should apply.Useage:
LINQ WhereIf At ExtensionMethod.NET and borrowed from Andrew's blog.
AssertCount
Efficiently determines if an an
IEnumerable<T>
contains at least / exactly / at most a certain number of elements.Usage: