IEnumerableExtend.cs 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /* ==============================================================================
  2. * 功能描述:Enumerable 扩展
  3. * 创 建 者:SAGACLOUD
  4. * 创建日期:2017/9/17
  5. * ==============================================================================*/
  6. using System;
  7. using System.Collections;
  8. using System.Collections.Generic;
  9. using System.Linq;
  10. namespace SAGA.DotNetUtils.Extend
  11. {
  12. public static class EnumerableExtend
  13. {
  14. public static int GetCount(this IEnumerable items)
  15. {
  16. return items.Cast<object>().Count();
  17. }
  18. public static object[] GetArrary(this IEnumerable items)
  19. {
  20. return items.Cast<object>().ToArray();
  21. }
  22. /// <summary>
  23. /// 获取T类型的数组
  24. /// </summary>
  25. /// <typeparam name="T"></typeparam>
  26. /// <param name="items"></param>
  27. /// <returns></returns>
  28. public static T[] GetArraryT<T>(this IEnumerable items)
  29. {
  30. return (from object value in items select (T) value).ToArray();
  31. }
  32. /// <summary>
  33. /// 获取指定项
  34. /// </summary>
  35. /// <param name="items"></param>
  36. /// <param name="intIndex"></param>
  37. /// <returns></returns>
  38. public static object GetByIndex(this IEnumerable items, int intIndex)
  39. {
  40. int index = 0;
  41. foreach (object value in items)
  42. {
  43. if (intIndex == index)
  44. {
  45. return value;
  46. }
  47. index++;
  48. }
  49. return null;
  50. }
  51. public static T GetByIndexT<T>(this IEnumerable items, int intIndex)
  52. {
  53. var item = items.GetByIndex(intIndex);
  54. return (T) item;
  55. }
  56. public static List<T> ToList<T>(this IEnumerable items)
  57. {
  58. return items.OfType<T>().ToList();
  59. }
  60. /// <summary>
  61. /// 非null,并且cout>0
  62. /// </summary>
  63. /// <param name="items"></param>
  64. /// <returns></returns>
  65. public static bool IsNotNullEmptyExt(this IEnumerable items)
  66. {
  67. return !items.IsNullOrEmptyExt();
  68. }
  69. public static bool IsNullOrEmptyExt(this IEnumerable items)
  70. {
  71. if (items == null) return true;
  72. if (items is string)
  73. {
  74. return string.IsNullOrEmpty((string)items);
  75. }
  76. return (items.GetCount() == 0);
  77. }
  78. }
  79. }