EfTools.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Linq.Expressions;
  5. using System.Reflection;
  6. using System.Threading.Tasks;
  7. using System.Linq.Dynamic;
  8. namespace SupplierWeb.Codes.EF
  9. {
  10. public static class EfTools
  11. {
  12. //public static IQueryable<T> OrderBy<T>(this IQueryable<T> source, Expression<Func<T, object>> keySelector, bool ascending)
  13. //{
  14. // var selectorBody = keySelector.Body;
  15. // // Strip the Convert expression
  16. // if (selectorBody.NodeType == ExpressionType.Convert)
  17. // selectorBody = ((UnaryExpression)selectorBody).Operand;
  18. // // Create dynamic lambda expression
  19. // var selector = Expression.Lambda(selectorBody, keySelector.Parameters);
  20. // // Generate the corresponding Queryable method call
  21. // var queryBody = Expression.Call(typeof(Queryable),
  22. // ascending ? "OrderBy" : "OrderByDescending",
  23. // new Type[] { typeof(T), selectorBody.Type },
  24. // source.Expression, Expression.Quote(selector));
  25. // return source.Provider.CreateQuery<T>(queryBody);
  26. //}
  27. //public static IOrderedQueryable<T> OrderBy<T>(
  28. // this IQueryable<T> query, string propertyName, bool ascending)
  29. //{
  30. // var entityType = typeof(T);
  31. // //Create x=>x.PropName
  32. // var propertyInfo = entityType.GetProperty(propertyName);
  33. // ParameterExpression arg = Expression.Parameter(entityType, "x");
  34. // MemberExpression property = Expression.Property(arg, propertyName);
  35. // var selector = Expression.Lambda(property, new ParameterExpression[] { arg });
  36. // //Get System.Linq.Queryable.OrderBy() method.
  37. // var enumarableType = typeof(Queryable);
  38. // var method = enumarableType.GetMethods()
  39. // .Where(m => m.Name == (ascending ? "OrderBy" : "OrderByDescending") && m.IsGenericMethodDefinition)
  40. // .Where(m =>
  41. // {
  42. // var parameters = m.GetParameters().ToList();
  43. // //Put more restriction here to ensure selecting the right overload
  44. // return parameters.Count == 2;//overload that has 2 parameters
  45. // }).Single();
  46. // //The linq's OrderBy<TSource, TKey> has two generic types, which provided here
  47. // MethodInfo genericMethod = method.MakeGenericMethod(entityType, propertyInfo.PropertyType);
  48. // /*Call query.OrderBy(selector), with query and selector: x=> x.PropName
  49. // Note that we pass the selector as Expression to the method and we don't compile it.
  50. // By doing so EF can extract "order by" columns and generate SQL for it.*/
  51. // var newQuery = (IOrderedQueryable<T>)genericMethod.Invoke(genericMethod, new object[] { query, selector });
  52. // return newQuery;
  53. //}
  54. //public static IQueryable<T> Where<T>(this IQueryable<T> query, List<WhereParam> whereParams)
  55. //{
  56. // var entityType = typeof(T);
  57. // var x = Expression.Parameter(entityType, "x");
  58. // foreach (var item in whereParams)
  59. // {
  60. // Expression.Equal()
  61. // }
  62. // query.OrderBy()
  63. //}
  64. }
  65. public struct WhereParam
  66. {
  67. public string Key { get; set; }
  68. public string Value { get; set; }
  69. }
  70. }