Groups items into same size lots. : IEnumerable « Collections Data Structure « C# / C Sharp

Home
C# / C Sharp
1.2D Graphics
2.Class Interface
3.Collections Data Structure
4.Components
5.Data Types
6.Database ADO.net
7.Date Time
8.Design Patterns
9.Development Class
10.Event
11.File Stream
12.Generics
13.GUI Windows Form
14.Internationalization I18N
15.Language Basics
16.LINQ
17.Network
18.Office
19.Reflection
20.Regular Expressions
21.Security
22.Services Event
23.Thread
24.Web Services
25.Windows
26.Windows Presentation Foundation
27.XML
28.XML LINQ
C# / C Sharp » Collections Data Structure » IEnumerable 




Groups items into same size lots.
        

using System.Linq;
using System.Diagnostics;
namespace System.Collections.Generic
{
    public static class EnumeratorExtensions
    {


        /// <summary>
        /// Groups items into same size lots.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="source">The source list of items.</param>
        /// <param name="size">The maximum size of the groups to make.</param>
        /// <returns>A list of list of items, where each list of items is no bigger than the size given.</returns>
        public static IEnumerable<IEnumerable<T>> Clump<T>(this IEnumerable<T> source, int size)
        {
            if (source == null)
                throw new ArgumentNullException("source");
            if (size < 1)
                throw new ArgumentOutOfRangeException("size""size must be greater than 0");

            return ClumpIterator<T>(source, size);
        }

        private static IEnumerable<IEnumerable<T>> ClumpIterator<T>(IEnumerable<T> source, int size)
        {
            Debug.Assert(source != null, "source is null.");

            T[] items = new T[size];
            int count = 0;
            foreach (var item in source)
            {
                items[count= item;
                count++;

                if (count == size)
                {
                    yield return items;
                    items = new T[size];
                    count = 0;
                }
            }
            if (count > 0)
            {
                if (count == size)
                    yield return items;
                else
                {
                    T[] tempItems = new T[count];
                    Array.Copy(items, tempItems, count);
                    yield return tempItems;
                }
            }
        }
    }
}

   
    
    
    
    
    
    
    
  














Related examples in the same category
1.Determines whether the specified collection is null or empty.
2.Determines whether the collection contains the specified element
3.Determines whether the collection contains all the elements in the specified collection.
4.Aggregate IEnumerable
5.Convert IEnumerable To DataTable
6.Convert IEnumerable by Func
7.Split IEnumerable
8.Shuffle IEnumerable
9.For each IEnumerable
10.Sleep before yield each element in IEnumerable
11.Add WhereIf to IEnumerable
12.Add OrderBy to IEnumerable
13.Add Repeat extension to IEnumerable
14.Yield element in IEnumerable until Predicate
15.IEnumerable extension for get second and third element
16.Simple function to determine if an item is an IEnumerable
17.Object class extension for IEnumerable, IComparable and where
18.Output DataTable and IEnumerable to console
19.Convert IEnumerable to String
20.Convert XmlNodeList to IEnumerable
21.Returns the first element contained in both, source and candidates.
22.Given a range (start,end) and a number of steps, will yield that a number for each step
23.Compares two sequences.
24.Empty Enumerable/Enumerator
25.CharEnumerator supports iterating over a String and reading its individual characters.
26.IEnumerable interface exposes the enumerator, which supports a simple iteration over a non-generic collection.
27.IEnumerator interface supports a simple iteration over a nongeneric collection.
28.Convert IEnumerable to ObservableCollection
29.IEnumerable To Comma String
30.Do all for IEnumerable and Action
31.Convert IEnumerable to IEnumerable
32.Filter IEnumerable with Predicate
33.Join IEnumerable
34.Mode value for IEnumerable
35.Build Statistics function on to IEnumerable
36.Compare two IEnumerable
37.Convert IEnumerable to HashSet
38.Concatenate IEnumerable to String
39.Singleton IEnumerable
40.Convert IEnumerable To ReadOnlyCollection
41.Performs the specified action on each element of the IEnumerable
42.Creates a list by applying a delegate to pairs of items in the IEnumerable
43.Returns true if there are enough items in the IEnumerable
44.Returns true if there are no more than a set number of items in the IEnumerable
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.