using System; using System.Collections.Generic; namespace DataModel.Helpers { /// /// LINQ helper methods /// public static class LinqHelpers { /// /// Filters for distinct elements by given key /// /// Source data type /// Key data type /// Source data /// Key value function /// Distinct elements from source public static IEnumerable DistinctBy(this IEnumerable source, Func keySelector) { HashSet keys = new HashSet(); foreach (TSource element in source) { if (keys.Add(keySelector(element))) { yield return element; } } } /// /// Groups the data into batches of give size /// /// Data type of data /// Data to break into batches /// Size of batches to create /// Enumerable collection of batches to process public static IEnumerable> BatchGroup(this IEnumerable allData, int batchSize) { List batchGroup = new List(); //Loop through data and group into batches foreach (T data in allData) { batchGroup.Add(data); if (batchGroup.Count == batchSize) { //Return batch of data yield return batchGroup; batchGroup = new List(); } } //Return remaining data yield return batchGroup; } } }