public static IQueryable <TEntity> Paginate <TEntity, TModel>(this IQueryable <TEntity> result, TModel options) where TModel : IQueryPaging { var attr = Attribute.GetCustomAttributes(PrimitiveExtensions.GetProperty <TModel>("Limit")).FirstOrDefault(); // Check for the AnimalType attribute. if (attr?.GetType() == typeof(QueryOperatorAttribute)) { var data = (QueryOperatorAttribute)attr; if (data.Max > 0) { options.Limit = data.Max; } } if (options.Offset.HasValue) { result = result.Skip(options.Offset.Value); } if (options.Limit.HasValue) { result = result.Take(options.Limit.Value); } return(result); }
public static IQueryable <TEntity> Paginate <TEntity, TModel>(this IQueryable <TEntity> result, TModel options) where TModel : class, IQueryPaging { var attr = Attribute.GetCustomAttribute(PrimitiveExtensions.GetProperty(options.GetType(), "Limit"), typeof(QueryOperatorAttribute)); if (attr?.GetType() == typeof(QueryOperatorAttribute)) { var data = (QueryOperatorAttribute)attr; if (data.Max > 0) { options.Limit = data.Max; } } if (options.Offset.HasValue) { result = result.Skip(options.Offset.Value); } if (options.Limit.HasValue) { result = result.Take(options.Limit.Value); } return(result); }
public static IQueryable <TEntity> Sort <TEntity>(this IQueryable <TEntity> result, string fields) { if (string.IsNullOrEmpty(fields)) { return(result); } var useThenBy = false; foreach (var sortTerm in fields.Fields()) { var property = PrimitiveExtensions.GetProperty <TEntity>(sortTerm.FieldName()); if (property != null) { var command = useThenBy ? "ThenBy" : "OrderBy"; command += sortTerm.IsDescending() ? "Descending" : string.Empty; result = result.OrderBy(property, command); } useThenBy = true; } return(result); }