public virtual async Task <List <TEntity> > GetWhereAsync(FilterPaginateDto filterPaginateDto) { var query = _entities.Select(x => x); if (filterPaginateDto.filters != null) { query = query.Where(QueryExpressionBuilder.GetExpression <TEntity>(filterPaginateDto.filters)); } query = query .Skip(filterPaginateDto.Skip) .Take(filterPaginateDto.Limit); var param = Expression.Parameter(typeof(TEntity), "t"); if (string.IsNullOrEmpty(filterPaginateDto.SortPropertyName)) { return(await query.ToListAsync()); } MemberExpression member = Expression.Property(param, filterPaginateDto.SortPropertyName); if (filterPaginateDto.Order == null) { filterPaginateDto.Order = Order.Asc; } switch (member.Type.Name) { case "Int32": var int32Expression = Expression.Lambda <Func <TEntity, Int32> >(member, param); if (filterPaginateDto.Order == Order.Asc) { return(await query.OrderBy(int32Expression).ToListAsync()); } else { return(await query.OrderByDescending(int32Expression).ToListAsync()); } case "String": var stringExpression = Expression.Lambda <Func <TEntity, string> >(member, param); if (filterPaginateDto.Order == Order.Asc) { return(await query.OrderBy(stringExpression).ToListAsync()); } else { return(await query.OrderByDescending(stringExpression).ToListAsync()); } case "DateTime": var dateTimeExpression = Expression.Lambda <Func <TEntity, string> >(member, param); if (filterPaginateDto.Order == Order.Asc) { return(await query.OrderBy(dateTimeExpression).ToListAsync()); } else { return(await query.OrderByDescending(dateTimeExpression).ToListAsync()); } case "Nullable`1": var nullableType = Nullable.GetUnderlyingType(member.Type); switch (nullableType.Name) { case "DateTime": var nullDateTimeExpression = Expression.Lambda <Func <TEntity, DateTime?> >(member, param); if (filterPaginateDto.Order == Order.Asc) { return(await query.OrderBy(nullDateTimeExpression).ToListAsync()); } else { return(await query.OrderByDescending(nullDateTimeExpression).ToListAsync()); } case "Int32": var nullInt32Expression = Expression.Lambda <Func <TEntity, Int32?> >(member, param); if (filterPaginateDto.Order == Order.Asc) { return(await query.OrderBy(nullInt32Expression).ToListAsync()); } else { return(await query.OrderByDescending(nullInt32Expression).ToListAsync()); } default: var nullObjExpression = Expression.Lambda <Func <TEntity, object> >(member, param); if (filterPaginateDto.Order == Order.Asc) { return(await query.OrderBy(nullObjExpression).ToListAsync()); } else { return(await query.OrderByDescending(nullObjExpression).ToListAsync()); } } default: var objExpression = Expression.Lambda <Func <TEntity, object> >(member, param); if (filterPaginateDto.Order == Order.Asc) { return(await query.OrderBy(objExpression).ToListAsync()); } else { return(await query.OrderByDescending(objExpression).ToListAsync()); } } }
public virtual async Task <int> CountWhereAsync(IEnumerable <FilterDto> filters) => await _entities.CountAsync(QueryExpressionBuilder.GetExpression <TEntity>(filters));