LinqS.cs 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. // 自定义Linq相关代码
  2. // 主要是一些CommonUseage代码
  3. //
  4. using System;
  5. using System.Collections.Generic;
  6. //
  7. //
  8. public static class LinqS
  9. {
  10. public static List<string> ToStringList(string[] InStringArray )
  11. {
  12. if( InStringArray == null )
  13. {
  14. return null;
  15. }
  16. List<string> Items = new List<string>(InStringArray.Length);
  17. for( int i=0; i<InStringArray.Length; ++i )
  18. {
  19. Items.Add(InStringArray[i]);
  20. }
  21. return Items;
  22. }
  23. public static string[] Skip( string[] InStringArray, int InCount )
  24. {
  25. if (InStringArray == null)
  26. {
  27. return null;
  28. }
  29. int Remain = InStringArray.Length - InCount;
  30. string[] Results = new string[Remain];
  31. for( int i=InCount; i<InStringArray.Length; ++i )
  32. {
  33. Results[i - InCount] = InStringArray[i];
  34. }
  35. return Results;
  36. }
  37. public static string[] Take( string[] InStringArray, int InCount )
  38. {
  39. if (InStringArray == null)
  40. {
  41. return null;
  42. }
  43. string[] Results = new string[InCount];
  44. for( int i=0; i<InCount && i < InStringArray.Length; ++i )
  45. {
  46. Results[i] = InStringArray[i];
  47. }
  48. return Results;
  49. }
  50. public static string[] Where(string[] InStringArray, Func<string, bool> InPredicate )
  51. {
  52. if (InStringArray == null)
  53. {
  54. return null;
  55. }
  56. List<string> Results = new List<string>(InStringArray.Length);
  57. for( int i=0; i<InStringArray.Length; ++i )
  58. {
  59. if( InPredicate(InStringArray[i]))
  60. {
  61. Results.Add(InStringArray[i]);
  62. }
  63. }
  64. return Results.ToArray();
  65. }
  66. public static bool Contains<T>( T[] InArray, T InTest )
  67. {
  68. if( InArray == null || InArray.Length == 0 )
  69. {
  70. return false;
  71. }
  72. for( int i=0; i<InArray.Length; ++i )
  73. {
  74. if( Comparer<T>.Equals(InArray[i], InTest ) )
  75. {
  76. return true;
  77. }
  78. }
  79. return false;
  80. }
  81. public static T[] ToArray<T>( List<T> InList )
  82. {
  83. if( InList == null )
  84. {
  85. return null;
  86. }
  87. T[] Results = new T[InList.Count];
  88. for( int i=0; i<InList.Count; ++i )
  89. {
  90. Results[i] = InList[i];
  91. }
  92. return Results;
  93. }
  94. }