QueryableExtensions.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Linq.Expressions;
  5. namespace Muchinfo.MTPClient.Infrastructure.Extensions
  6. {
  7. /// <summary>
  8. /// QueryableExtensions类
  9. /// </summary>
  10. public static class QueryableExtensions
  11. {
  12. #region Methods
  13. #region Public Static Methods
  14. /// <summary>
  15. /// 属性名排序
  16. /// </summary>
  17. /// <typeparam name="T"></typeparam>
  18. /// <param name="queryable">The queryable.</param>
  19. /// <param name="propertyName">Name of the property.</param>
  20. /// <returns>IQueryable{``0}.</returns>
  21. public static IQueryable<T> OrderBy<T>(this IQueryable<T> queryable, string propertyName)
  22. {
  23. return QueryableHelper<T>.OrderBy(queryable, propertyName);
  24. }
  25. /// <summary>
  26. /// 属性名倒排序
  27. /// </summary>
  28. /// <typeparam name="T"></typeparam>
  29. /// <param name="queryable">The queryable.</param>
  30. /// <param name="propertyName">Name of the property.</param>
  31. /// <returns>IQueryable{``0}.</returns>
  32. public static IQueryable<T> OrderByDescending<T>(this IQueryable<T> queryable, string propertyName)
  33. {
  34. return QueryableHelper<T>.OrderByDescending(queryable, propertyName);
  35. }
  36. /// <summary>
  37. /// 数据随机排序
  38. /// </summary>
  39. /// <typeparam name="T">泛型类型</typeparam>
  40. /// <param name="source">源数组</param>
  41. /// <returns>特定数组.</returns>
  42. public static T[] ToRandomArray<T>(this T[] source)
  43. {
  44. if (source == null || source.Length == 0)
  45. {
  46. return new T[] { };
  47. }
  48. var dir = new Dictionary<int, T>();
  49. var rd = new Random(unchecked((int)DateTime.Now.Ticks));
  50. var count = source.Length;
  51. foreach (var value in source)
  52. {
  53. var index = rd.Next(1, count + 1000);
  54. while (dir.Keys.Contains(index))
  55. {
  56. index = rd.Next(1, count + 1000);
  57. }
  58. dir.Add(index, value);
  59. }
  60. return dir.Keys.OrderBy(z => z).Select(key => dir[key]).ToArray();
  61. }
  62. #endregion Public Static Methods
  63. #endregion Methods
  64. #region Nested Types
  65. /// <summary>
  66. /// QueryableHelper类
  67. /// </summary>
  68. /// <typeparam name="T"></typeparam>
  69. private static class QueryableHelper<T>
  70. {
  71. #region Fields
  72. /// <summary>
  73. /// The _cache
  74. /// </summary>
  75. public static Dictionary<string, LambdaExpression> _cache = new Dictionary<string, LambdaExpression>();
  76. #endregion Fields
  77. #region Methods
  78. #region Public Static Methods
  79. /// <summary>
  80. /// Orders the by.
  81. /// </summary>
  82. /// <param name="queryable">The queryable.</param>
  83. /// <param name="propertyName">The properytname.</param>
  84. /// <returns>IQueryable{`0}.</returns>
  85. public static IQueryable<T> OrderBy(IQueryable<T> queryable, string propertyName)
  86. {
  87. dynamic keySelector = GetLambdaExpression(propertyName);
  88. return Queryable.OrderBy(queryable, keySelector);
  89. }
  90. /// <summary>
  91. /// Orders the by descending.
  92. /// </summary>
  93. /// <param name="queryable">The queryable.</param>
  94. /// <param name="propertyName">Name of the properyt.</param>
  95. /// <returns>IQueryable{`0}.</returns>
  96. public static IQueryable<T> OrderByDescending(IQueryable<T> queryable, string propertyName)
  97. {
  98. dynamic keySelector = GetLambdaExpression(propertyName);
  99. return Queryable.OrderByDescending(queryable, keySelector);
  100. }
  101. #endregion Public Static Methods
  102. #region Private Static Methods
  103. /// <summary>
  104. /// Gets the lambda expression.
  105. /// </summary>
  106. /// <param name="propertyName">Name of the property.</param>
  107. /// <returns>LambdaExpression.</returns>
  108. private static LambdaExpression GetLambdaExpression(string propertyName)
  109. {
  110. if (_cache.ContainsKey(propertyName))
  111. {
  112. return _cache[propertyName];
  113. }
  114. var param = Expression.Parameter(typeof(T));
  115. var body = Expression.Property(param, propertyName);
  116. var keySelector = Expression.Lambda(body, param);
  117. _cache[propertyName] = keySelector;
  118. return keySelector;
  119. }
  120. #endregion Private Static Methods
  121. #endregion Methods
  122. }
  123. #endregion Nested Types
  124. }
  125. }