Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- return persons.Where(p => p.Name.StartsWith(filterModel.Term ?? String.Empty, StringComparison.InvariantCultureIgnoreCase))
- .OrderBy(c=>c.Name)
- .Skip((filterModel.Page-1) * filter.Limit)
- .Take(filterModel.Limit);
- public static class PaginateClass
- {
- static readonly MethodInfo startsWith = typeof(string).GetMethod("StartsWith", new[] { typeof(string), typeof(System.StringComparison) });
- public static IEnumerable<T> Paginate<T>(this IEnumerable<T> input, PageModel pageModel, string columnName) where T : class
- {
- var type = typeof(T);
- var propertyInfo = type.GetProperty(columnName);
- //T p =>
- var parameter = Expression.Parameter(type, "p");
- //T p => p.ColumnName
- var name = Expression.Property(parameter, propertyInfo);
- // filterModel.Term ?? String.Empty
- var term = Expression.Constant(pageModel.Term ?? String.Empty);
- //StringComparison.InvariantCultureIgnoreCase
- var comparison = Expression.Constant(StringComparison.InvariantCultureIgnoreCase);
- //T p => p.ColumnName.StartsWith(filterModel.Term ?? String.Empty, StringComparison.InvariantCultureIgnoreCase)
- var methodCall = Expression.Call(name, startsWith, term, comparison);
- var lambda = Expression.Lambda<Func<T, bool>>(methodCall, parameter);
- return input.Where(lambda.Compile()) //tried adding this and did not work .OrderBy(parameter)
- .Skip((pageModel.Page - 1) * pageModel.Limit)
- .Take(pageModel.Limit);
- }
- public class PageModel
- {
- public int Page { get; set; }
- public int Limit { get; set; }
- public string Term { get; set; }
- public PageModel()
- {
- this.Page = 1;
- this.Limit = 3;
- }
- public object Clone()
- {
- var jsonString = JsonConvert.SerializeObject(this);
- return JsonConvert.DeserializeObject(jsonString, this.GetType());
- }
- }
- void Main()
- {
- var queryableRecords = Product.FetchQueryableProducts();
- Expression expression = queryableRecords.OrderBy("Name");
- var func = Expression.Lambda<Func<IQueryable<Product>>>(expression)
- .Compile();
- func().Dump();
- }
- public class Product
- {
- public int Id { get; set; }
- public string Name { get; set; }
- public static IQueryable<Product> FetchQueryableProducts()
- {
- List<Product> productList = new List<Product>()
- {
- new Product {Id=1, Name = "A"},
- new Product {Id=1, Name = "B"},
- new Product {Id=1, Name = "A"},
- new Product {Id=2, Name = "C"},
- new Product {Id=2, Name = "B"},
- new Product {Id=2, Name = "C"},
- };
- return productList.AsQueryable();
- }
- }
- public static class ExpressionTreesExtesion
- {
- public static Expression OrderBy(this IQueryable queryable, string propertyName)
- {
- var propInfo = queryable.ElementType.GetProperty(propertyName);
- var collectionType = queryable.ElementType;
- var parameterExpression = Expression.Parameter(collectionType, "g");
- var propertyAccess = Expression.MakeMemberAccess(parameterExpression, propInfo);
- var orderLambda = Expression.Lambda(propertyAccess, parameterExpression);
- return Expression.Call(typeof(Queryable),
- "OrderBy",
- new Type[] { collectionType, propInfo.PropertyType },
- queryable.Expression,
- Expression.Quote(orderLambda));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement