IEnumerable扩展方法

// /***********************************************************
// *  项目名称:   YunDouTax.BaseLib
// *  文件名称:   EnumerableExtension.cs
// *  功能描述:   
// *  代码作者:   云勇
// *  创建时间:   2017年12月25日 11:50  
// *  更新时间:   2018年01月01日 21:12
// ************************************************************/

namespace YunDouTax.BaseLib.Extensions
{
    /// 
    /// 提供一组用于查询实现 System.Collections.Generic.IEnumerable`1 的对象的 static 方法。
    /// 
    public static class EnumerableExtension
    {
        /// 
        /// 通过合并元素的索引将序列的每个元素投影到新表中。
        /// 
        /// The type of the source.
        /// The type of the result.
        /// The source.
        /// The selector.
        /// 
        /// selector cann't be null.
        public static IEnumerable Select(this IEnumerable source, Func selector)
        {
            if (selector == null)
            {
                throw new ArgumentNullException("selector cann't be null.");
            }

            foreach (TSource item in source)
            {
                if (item == null)
                {
                    continue;
                }

                TResult result = selector(item);
                if (result == null)
                {
                    continue;
                }

                yield return result;
            }
        }

        /// 
        /// 基于谓词筛选值序列。
        /// 
        /// The type of the source.
        /// The source.
        /// The predicate.
        /// 
        /// predicate cann't be null.
        public static IEnumerable Where(this IEnumerable source, Func predicate)
        {
            if (predicate == null)
            {
                throw new ArgumentNullException("predicate cann't be null.");
            }

            foreach (TSource item in source)
            {
                if (predicate(item))
                {
                    yield return item;
                }
            }
        }

        /// 
        /// 返回序列中的元素数量。
        /// 
        /// The type of the source.
        /// The source.
        /// 
        public static int Count(this IEnumerable source)
        {
            int count = 0;
            foreach (TSource item in source)
            {
                count++;
            }

            return count;
        }

        /// 
        /// 返回一个数字,表示在指定的序列中满足条件的元素数量。
        /// 
        /// The type of the source.
        /// The source.
        /// The predicate.
        /// 
        /// predicate cann't be null.
        public static int Count(this IEnumerable source, Func predicate)
        {
            if (predicate == null)
            {
                throw new ArgumentNullException("predicate cann't be null.");
            }

            int count = 0;
            foreach (TSource item in source)
            {
                if (predicate(item))
                {
                    count++;
                }
            }

            return count;
        }

        /// 
        /// 返回序列中的第一个元素;如果序列中不包含任何元素,则返回默认值。
        /// 
        /// The type of the source.
        /// The source.
        /// The predicate.
        /// 
        public static TSource FirstOrDefault(this IEnumerable source, Func predicate = null)
        {
            if (source == null)
            {
                return default(TSource);
            }

            foreach (TSource item in source)
            {
                if (predicate == null)
                {
                    return item;
                }

                if (predicate != null && predicate(item))
                {
                    return item;
                }
            }

            return default(TSource);
        }

        /// 
        /// 从 System.Collections.Generic.IEnumerable`1 创建一个 System.Collections.Generic.List`1。
        /// 
        /// The type of the source.
        /// The source.
        /// 
        public static List ToList(this IEnumerable source)
        {
            return new List(source);
        }

        /// 
        /// 从 System.Collections.Generic.IEnumerable`1 创建一个数组。
        /// 
        /// The type of the source.
        /// The source.
        /// 
        public static TSource[] ToArray(this IEnumerable source)
        {
            TSource[] array = new TSource[source.Count()];

            int index = 0;
            foreach (TSource item in source)
            {
                array[index++] = item;
            }

            return array;
        }

        /// 
        /// 通过使用默认的相等比较器确定序列是否包含指定的元素。
        /// 
        /// The type of the source.
        /// The source.
        /// The value.
        /// 
        ///   true if [contains] [the specified value]; otherwise, false.
        /// 
        public static bool Contains(this IEnumerable source, TSource value)
        {
            foreach (TSource item in source)
            {
                if (item.Equals(value))
                {
                    return true;
                }
            }

            return false;
        }

        /// 
        /// 判断指定的序列是否为空
        /// 
        /// The type of the source.
        /// The source.
        /// 
        ///   true if the specified source is empty; otherwise, false.
        /// 
        public static bool IsEmpty(this IEnumerable source)
        {
            return source == null || source.Count() == 0;
        }

        /// 
        /// 根据键按升序对序列的元素排序
        /// 
        public static IEnumerable OrderBy(this IEnumerable source, Func keySelector) where TKey : IComparable
        {
            List ls = source.ToList();
            for (int i = 0; i < ls.Count; i++)
            {
                TKey min = keySelector(ls[i]);
                for (int j = i + 1; j < ls.Count; j++)
                {
                    TKey key = keySelector(ls[j]);
                    if (min.CompareTo(key) > 0)
                    {
                        min = key;

                        TSource tmp = ls[i];
                        ls[i] = ls[j];
                        ls[j] = tmp;
                    }
                }

                yield return ls[i];
            }
        }


        /// 
        /// Alternates the specified first.
        /// 
        /// The type of the source.
        /// The first.
        /// The second.
        /// 
        public static IEnumerable Alternate(this IEnumerable first, IEnumerable second)
        {
            using (IEnumerator e1 = first.GetEnumerator())
            using (IEnumerator e2 = second.GetEnumerator())
            {
                while (e1.MoveNext() && e2.MoveNext())
                {
                    yield return e1.Current;
                    yield return e2.Current;
                }
            }
        }

        /// 
        /// Appends the specified source.
        /// 
        /// The type of the source.
        /// The source.
        /// The element.
        /// IEnumerable
        public static IEnumerable Append(this IEnumerable source, TSource element)
        {
            using (IEnumerator e1 = source.GetEnumerator())
            {
                while (e1.MoveNext())
                {
                    yield return e1.Current;
                }
            }

            yield return element;
        }

        /// 
        /// Determines whether [contains] [the specified source].
        /// 
        /// The type of the source.
        /// The type of the result.
        /// The source.
        /// The value.
        /// The selector.
        /// 
        ///     true if [contains] [the specified source]; otherwise, false.
        /// 
        public static bool Contains(this IEnumerable source, TResult value, Func selector)
        {
            foreach (TSource sourceItem in source)
            {
                TResult sourceValue = selector(sourceItem);
                if (sourceValue.Equals(value))
                {
                    return true;
                }
            }

            return false;
        }

        /// 
        /// Prepends the specified source.
        /// 
        /// The type of the source.
        /// The source.
        /// The element.
        /// IEnumerable
        public static IEnumerable Prepend(this IEnumerable source, TSource element)
        {
            yield return element;

            using (IEnumerator e1 = source.GetEnumerator())
            {
                while (e1.MoveNext())
                {
                    yield return e1.Current;
                }
            }
        }
    }
}

你可能感兴趣的:(winform,技巧共享,C#常用工具类与开发技巧)