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.
ToQueue & ToStack
Duplicates
Used in conjunction with a method like Ani's
AssertCount
method (I use one calledCountAtLeast
), it becomes very easy to find elements in a sequence that appear more than once:RandomSample
Here's a simple function that's useful if you have a medium-large set of data (say, over 100 items) and you want to eyeball just a random sampling of it.
Usage:
Notes:
JoinString
Basically the same as
string.Join
, but:with the ability to use it on any collection, not just a collection of strings (calls
ToString
on every element)with the ability to add a prefix and suffix to every string.
as an extension method. I find
string.Join
annoying because it is static, meaning that in a chain of operations it is lexically not in the correct order.Chunks
Returns chunks of a specific size.
x.Chunks(2)
of1,2,3,4,5
will return two arrays with1,2
and3,4
.x.Chunks(2,true)
will return1,2
,3,4
and5
.ToHashSet