Skip to content

Commit

Permalink
Merge pull request #13 from RicardoZambon/features/query-filter-with-…
Browse files Browse the repository at this point in the history
…value

feat: Add method to filter queries with arbitrary values
  • Loading branch information
RicardoZambon authored May 14, 2024
2 parents 8aa7e4d + 0f5a82c commit 381e6de
Showing 1 changed file with 52 additions and 27 deletions.
79 changes: 52 additions & 27 deletions ZWebAPI/ExtensionMethods/QueryableExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -120,37 +120,29 @@ public static IQueryable<TEntity> TryFilter<TEntity>(this IQueryable<TEntity> qu
}
}

if (parameters.HasFilter(parameterName) && filterProperty != null)
if (parameters.HasFilter(parameterName) && filterProperty is not null)
{
object? filterValue = parameters.GetFilterValue(parameterName, filterProperty.PropertyType);

Expression<Func<TEntity, bool>>? where = null;
switch (filterType)
{
case FilterTypes.Like:
where = BuildLikeExpression<TEntity>(property, filterValue);
break;
case FilterTypes.Equals:
where = BuildEqualsExpression<TEntity>(property, filterValue);
break;
case FilterTypes.LessThan:
where = BuildLessThanExpression<TEntity>(property, filterValue);
break;
case FilterTypes.LessThanOrEqual:
where = BuildLessThanOrEqualExpression<TEntity>(property, filterValue);
break;
case FilterTypes.GreaterThan:
where = BuildGreaterThanExpression<TEntity>(property, filterValue);
break;
case FilterTypes.GreatherThanOrEqual:
where = BuildGreaterThanOrEqualExpression<TEntity>(property, filterValue);
break;
}
return RunFilter(query, property, filterValue, filterType);
}
return query;
}

if (where != null)
{
return query.Where(where);
}
/// <summary>
/// Tries to filter the query with arbitrary value.
/// </summary>
/// <typeparam name="TEntity">The type of the entity.</typeparam>
/// <param name="query">The query.</param>
/// <param name="property">The property.</param>
/// <param name="filterValue">The filter value.</param>
/// <param name="filterType">Type of the filter.</param>
/// <returns>Return the query filtered.</returns>
public static IQueryable<TEntity> TryFilterWithValue<TEntity>(this IQueryable<TEntity> query, string property, object? filterValue, FilterTypes filterType)
{
if (filterValue is not null)
{
return RunFilter(query, property, filterValue, filterType);
}
return query;
}
Expand Down Expand Up @@ -268,6 +260,39 @@ private static ParameterExpression GetParameter<TEntity>()

return expression;
}

private static IQueryable<TEntity> RunFilter<TEntity>(this IQueryable<TEntity> query, string property, object? filterValue, FilterTypes filterType)
{
Expression<Func<TEntity, bool>>? where = null;
switch (filterType)
{
case FilterTypes.Like:
where = BuildLikeExpression<TEntity>(property, filterValue);
break;
case FilterTypes.Equals:
where = BuildEqualsExpression<TEntity>(property, filterValue);
break;
case FilterTypes.LessThan:
where = BuildLessThanExpression<TEntity>(property, filterValue);
break;
case FilterTypes.LessThanOrEqual:
where = BuildLessThanOrEqualExpression<TEntity>(property, filterValue);
break;
case FilterTypes.GreaterThan:
where = BuildGreaterThanExpression<TEntity>(property, filterValue);
break;
case FilterTypes.GreatherThanOrEqual:
where = BuildGreaterThanOrEqualExpression<TEntity>(property, filterValue);
break;
}

if (where is not null)
{
return query.Where(where);
}

return query;
}
#endregion
}
}

0 comments on commit 381e6de

Please sign in to comment.