• 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();
                }
            }
        }
    }
    

      

  • 相关阅读:
    python RabbitMQ gRPC 实践经验
    python 数据库实践经验
    python GUI相关
    angularjs中的$watch、$digest、$apply
    css选择器优先级
    angularjs基本理解
    如何编写符合web标准的XHTML文档
    跨域问题
    如何养成良好的书写代码习惯
    ie常见兼容问题
  • 原文地址:https://www.cnblogs.com/ChobitsSP/p/3741357.html
Copyright © 2020-2023  润新知