| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Linq.Expressions;
- namespace Muchinfo.MTPClient.Infrastructure.Extensions
- {
- /// <summary>
- /// QueryableExtensions类
- /// </summary>
- public static class QueryableExtensions
- {
- #region Methods
- #region Public Static Methods
- /// <summary>
- /// 属性名排序
- /// </summary>
- /// <typeparam name="T"></typeparam>
- /// <param name="queryable">The queryable.</param>
- /// <param name="propertyName">Name of the property.</param>
- /// <returns>IQueryable{``0}.</returns>
- public static IQueryable<T> OrderBy<T>(this IQueryable<T> queryable, string propertyName)
- {
- return QueryableHelper<T>.OrderBy(queryable, propertyName);
- }
- /// <summary>
- /// 属性名倒排序
- /// </summary>
- /// <typeparam name="T"></typeparam>
- /// <param name="queryable">The queryable.</param>
- /// <param name="propertyName">Name of the property.</param>
- /// <returns>IQueryable{``0}.</returns>
- public static IQueryable<T> OrderByDescending<T>(this IQueryable<T> queryable, string propertyName)
- {
- return QueryableHelper<T>.OrderByDescending(queryable, propertyName);
- }
- /// <summary>
- /// 数据随机排序
- /// </summary>
- /// <typeparam name="T">泛型类型</typeparam>
- /// <param name="source">源数组</param>
- /// <returns>特定数组.</returns>
- public static T[] ToRandomArray<T>(this T[] source)
- {
- if (source == null || source.Length == 0)
- {
- return new T[] { };
- }
- var dir = new Dictionary<int, T>();
- var rd = new Random(unchecked((int)DateTime.Now.Ticks));
- var count = source.Length;
- foreach (var value in source)
- {
- var index = rd.Next(1, count + 1000);
- while (dir.Keys.Contains(index))
- {
- index = rd.Next(1, count + 1000);
- }
- dir.Add(index, value);
- }
- return dir.Keys.OrderBy(z => z).Select(key => dir[key]).ToArray();
- }
- #endregion Public Static Methods
- #endregion Methods
- #region Nested Types
- /// <summary>
- /// QueryableHelper类
- /// </summary>
- /// <typeparam name="T"></typeparam>
- private static class QueryableHelper<T>
- {
- #region Fields
- /// <summary>
- /// The _cache
- /// </summary>
- public static Dictionary<string, LambdaExpression> _cache = new Dictionary<string, LambdaExpression>();
- #endregion Fields
- #region Methods
- #region Public Static Methods
- /// <summary>
- /// Orders the by.
- /// </summary>
- /// <param name="queryable">The queryable.</param>
- /// <param name="propertyName">The properytname.</param>
- /// <returns>IQueryable{`0}.</returns>
- public static IQueryable<T> OrderBy(IQueryable<T> queryable, string propertyName)
- {
- dynamic keySelector = GetLambdaExpression(propertyName);
- return Queryable.OrderBy(queryable, keySelector);
- }
- /// <summary>
- /// Orders the by descending.
- /// </summary>
- /// <param name="queryable">The queryable.</param>
- /// <param name="propertyName">Name of the properyt.</param>
- /// <returns>IQueryable{`0}.</returns>
- public static IQueryable<T> OrderByDescending(IQueryable<T> queryable, string propertyName)
- {
- dynamic keySelector = GetLambdaExpression(propertyName);
- return Queryable.OrderByDescending(queryable, keySelector);
- }
- #endregion Public Static Methods
- #region Private Static Methods
- /// <summary>
- /// Gets the lambda expression.
- /// </summary>
- /// <param name="propertyName">Name of the property.</param>
- /// <returns>LambdaExpression.</returns>
- private static LambdaExpression GetLambdaExpression(string propertyName)
- {
- if (_cache.ContainsKey(propertyName))
- {
- return _cache[propertyName];
- }
- var param = Expression.Parameter(typeof(T));
- var body = Expression.Property(param, propertyName);
- var keySelector = Expression.Lambda(body, param);
- _cache[propertyName] = keySelector;
- return keySelector;
- }
- #endregion Private Static Methods
- #endregion Methods
- }
- #endregion Nested Types
- }
- }
|