LinqExtensions

using System;

using System.Linq;



namespace System.Collections.Generic

{

    public static class LinqExtensions

    {

        public static void ForEach<T>(this IEnumerable<T> array, Action<T> action)

        {

            foreach (T item in array)

            {

                if (action != null)

                {

                    action(item);

                }

            }

        }



        public static IEnumerable<T> AsEnumerable<T>(this IEnumerable array)

        {

            foreach (var item in array)

            {

                if (item is T)

                {

                    yield return (T)item;

                }

            }

        }



        /// <summary>

        /// 对枚举进行分组

        /// </summary>

        public static IEnumerable<T[]> GroupArray<T>(this IEnumerable<T> arr, int count)

        {

            List<T> temp = new List<T>();

            foreach (T item in arr)

            {

                if (temp.Count == count)

                {

                    yield return temp.ToArray();

                    temp.Clear();

                }

                temp.Add(item);

            }

            if (temp.Count > 0) yield return temp.ToArray();

        }



        public static IEnumerable<T[]> SplitContent<T>(this T[] array, int count)

        {

            int page_count = (int)Math.Ceiling((double)array.Length / count);

            for (int i = 0; i < page_count; i++)

            {

                int size = i != page_count - 1 ? count : array.Length - i * count;

                T[] temp = new T[size];

                Array.Copy(array, i * count, temp, 0, size);

                yield return temp;

                //yield return array.Skip(i * count).Take(count).ToArray();

            }

        }

    }

}

  

你可能感兴趣的:(LINQ)