/LinqExtensions

A few possibly-handy Linq extension methods that have been done many times before in different ways.

Primary LanguageC#MIT LicenseMIT

Build status Nuget

I'm certain there are many full-featured and excellent Linq extension libraries out there. In fact morelinq comes to mind, though I haven't actually used it. As usual, this is something I simply enjoy working on, and wanted more practice setting up builds in AppVeyor.

My other motivation for creating this is that it helped me build some examples for the chapter I'm writing on Linq for Packt's upcoming title The C# Workshop to which I'm contributing.

AO.Linq.Extensions.IEnumerableExtensions IEnumerableExtensions.cs

Methods

  • ILookup<int, T> Paginate (this IEnumerable items, int itemsPerPage) Splits a list into equal-sized "pages", maintinaing original item order. The last page can be shorter than the rest. See test1, test2, test3
  • ILookup<int, T> Partition (this IEnumerable items, int partitionCount) Splits a list into a fixed number of shorter lists of mostly equal length. Odd partitions will pad the "center" partition. See test

I went looking for better or compelling Paginate implementations, and this SO answer is probably my favorite. I found another very similar approach used here. My approach uses modulus, and for whatever reason I like using ILookup as the result.

The Partition method has stumped me in the past, and I wanted to write my own version of it. I got some ideas from this SO question, but I didn't care for any of the solutions. So, like I said, this is stuff I like to try.