Advertisement
Guest User

Untitled

a guest
Aug 21st, 2019
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.51 KB | None | 0 0
  1. return persons.Where(p => p.Name.StartsWith(filterModel.Term ?? String.Empty, StringComparison.InvariantCultureIgnoreCase))
  2. .OrderBy(c=>c.Name)
  3. .Skip((filterModel.Page-1) * filter.Limit)
  4. .Take(filterModel.Limit);
  5.  
  6.  
  7. public static class PaginateClass
  8. {
  9. static readonly MethodInfo startsWith = typeof(string).GetMethod("StartsWith", new[] { typeof(string), typeof(System.StringComparison) });
  10.  
  11. public static IEnumerable<T> Paginate<T>(this IEnumerable<T> input, PageModel pageModel, string columnName) where T : class
  12. {
  13. var type = typeof(T);
  14. var propertyInfo = type.GetProperty(columnName);
  15. //T p =>
  16. var parameter = Expression.Parameter(type, "p");
  17. //T p => p.ColumnName
  18. var name = Expression.Property(parameter, propertyInfo);
  19. // filterModel.Term ?? String.Empty
  20. var term = Expression.Constant(pageModel.Term ?? String.Empty);
  21. //StringComparison.InvariantCultureIgnoreCase
  22. var comparison = Expression.Constant(StringComparison.InvariantCultureIgnoreCase);
  23. //T p => p.ColumnName.StartsWith(filterModel.Term ?? String.Empty, StringComparison.InvariantCultureIgnoreCase)
  24. var methodCall = Expression.Call(name, startsWith, term, comparison);
  25.  
  26. var lambda = Expression.Lambda<Func<T, bool>>(methodCall, parameter);
  27.  
  28.  
  29. return input.Where(lambda.Compile()) //tried adding this and did not work .OrderBy(parameter)
  30. .Skip((pageModel.Page - 1) * pageModel.Limit)
  31. .Take(pageModel.Limit);
  32.  
  33. }
  34.  
  35. public class PageModel
  36. {
  37.  
  38. public int Page { get; set; }
  39. public int Limit { get; set; }
  40. public string Term { get; set; }
  41.  
  42. public PageModel()
  43. {
  44. this.Page = 1;
  45. this.Limit = 3;
  46. }
  47.  
  48. public object Clone()
  49. {
  50. var jsonString = JsonConvert.SerializeObject(this);
  51. return JsonConvert.DeserializeObject(jsonString, this.GetType());
  52. }
  53. }
  54.  
  55. void Main()
  56. {
  57. var queryableRecords = Product.FetchQueryableProducts();
  58.  
  59. Expression expression = queryableRecords.OrderBy("Name");
  60.  
  61. var func = Expression.Lambda<Func<IQueryable<Product>>>(expression)
  62. .Compile();
  63.  
  64. func().Dump();
  65. }
  66.  
  67. public class Product
  68. {
  69. public int Id { get; set; }
  70.  
  71. public string Name { get; set; }
  72.  
  73. public static IQueryable<Product> FetchQueryableProducts()
  74. {
  75. List<Product> productList = new List<Product>()
  76. {
  77. new Product {Id=1, Name = "A"},
  78. new Product {Id=1, Name = "B"},
  79. new Product {Id=1, Name = "A"},
  80. new Product {Id=2, Name = "C"},
  81. new Product {Id=2, Name = "B"},
  82. new Product {Id=2, Name = "C"},
  83. };
  84.  
  85. return productList.AsQueryable();
  86. }
  87. }
  88.  
  89. public static class ExpressionTreesExtesion
  90. {
  91.  
  92. public static Expression OrderBy(this IQueryable queryable, string propertyName)
  93. {
  94. var propInfo = queryable.ElementType.GetProperty(propertyName);
  95.  
  96. var collectionType = queryable.ElementType;
  97.  
  98. var parameterExpression = Expression.Parameter(collectionType, "g");
  99. var propertyAccess = Expression.MakeMemberAccess(parameterExpression, propInfo);
  100. var orderLambda = Expression.Lambda(propertyAccess, parameterExpression);
  101. return Expression.Call(typeof(Queryable),
  102. "OrderBy",
  103. new Type[] { collectionType, propInfo.PropertyType },
  104. queryable.Expression,
  105. Expression.Quote(orderLambda));
  106.  
  107. }
  108.  
  109.  
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement