public FilteredCollection <TEntity> GetAll(FilterPagingCriteria pageCriteria, FilterSortCriteria sortCriteria = null) { IQueryable <TEntity> collection = _context.Set <TEntity>().AppendFilterSortCriteria <TEntity, TId>(sortCriteria).AppendFilterPagingCriteria <TEntity, TId>(pageCriteria); return(new FilteredCollection <TEntity> { Collection = collection, TotalCount = Count() }); }
public static IQueryable <TEntity> AppendFilterSortCriteria <TEntity, TId>(this IQueryable <TEntity> query, FilterSortCriteria sortCriteria) where TEntity : IEntity <TId> { // Determine order command string command = sortCriteria != null ? (sortCriteria.Direction == SortDirection.DESC ? "OrderByDescending" : "OrderBy") : "OrderBy"; // Type of TEntity Type type = typeof(TEntity); // Get property by it's name PropertyInfo property = type.GetProperty(sortCriteria != null ? sortCriteria.Field : "Id"); // Create expression paramter ParameterExpression paramter = Expression.Parameter(type, "p"); // Create member expression MemberExpression propertyAccess = Expression.MakeMemberAccess(paramter, property); // Create lambda expression LambdaExpression orderByExpression = Expression.Lambda(propertyAccess, paramter); // Calling expression MethodCallExpression resultExpression = Expression.Call(typeof(Queryable), command, new Type[] { type, property.PropertyType }, query.Expression, Expression.Quote(orderByExpression)); // Construct the query based on order command return(query.Provider.CreateQuery <TEntity>(resultExpression)); }