| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788 |
- /* ==============================================================================
- * 功能描述:Enumerable 扩展
- * 创 建 者:SAGACLOUD
- * 创建日期:2017/9/17
- * ==============================================================================*/
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.Linq;
- namespace SAGA.DotNetUtils.Extend
- {
- public static class EnumerableExtend
- {
- public static int GetCount(this IEnumerable items)
- {
- return items.Cast<object>().Count();
- }
- public static object[] GetArrary(this IEnumerable items)
- {
- return items.Cast<object>().ToArray();
- }
- /// <summary>
- /// 获取T类型的数组
- /// </summary>
- /// <typeparam name="T"></typeparam>
- /// <param name="items"></param>
- /// <returns></returns>
- public static T[] GetArraryT<T>(this IEnumerable items)
- {
- return (from object value in items select (T) value).ToArray();
- }
- /// <summary>
- /// 获取指定项
- /// </summary>
- /// <param name="items"></param>
- /// <param name="intIndex"></param>
- /// <returns></returns>
- public static object GetByIndex(this IEnumerable items, int intIndex)
- {
- int index = 0;
- foreach (object value in items)
- {
- if (intIndex == index)
- {
- return value;
- }
- index++;
- }
- return null;
- }
- public static T GetByIndexT<T>(this IEnumerable items, int intIndex)
- {
- var item = items.GetByIndex(intIndex);
- return (T) item;
- }
- public static List<T> ToList<T>(this IEnumerable items)
- {
- return items.OfType<T>().ToList();
- }
- /// <summary>
- /// 非null,并且cout>0
- /// </summary>
- /// <param name="items"></param>
- /// <returns></returns>
- public static bool IsNotNullEmptyExt(this IEnumerable items)
- {
- return !items.IsNullOrEmptyExt();
- }
- public static bool IsNullOrEmptyExt(this IEnumerable items)
- {
- if (items == null) return true;
- if (items is string)
- {
- return string.IsNullOrEmpty((string)items);
- }
- return (items.GetCount() == 0);
- }
- }
- }
|