zoukankan      html  css  js  c++  java
  • 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();
                }
            }
        }
    }
    

      

  • 相关阅读:
    go test 下篇
    go test 上篇
    利用Docker Compose快速搭建本地测试环境
    phinx:php数据库迁移
    tp5 r3 一个简单的SQL语句调试实例
    TP开发小技巧
    优酷真实视频地址解析——2014年10月7日
    霍夫变换
    Google Earth影像数据破解之旅
    线程理论:(四)锁优化
  • 原文地址:https://www.cnblogs.com/ChobitsSP/p/3741357.html
Copyright © 2011-2022 走看看