Skip to content
This repository was archived by the owner on Feb 3, 2020. It is now read-only.

Repository files navigation

Azure DevOps buildsNuGet

Query Utils

Query Utils is extension for IQueryable to perform Paging, Filer and Sorting operations

Usage

Query Utils extension currently supports the following types of data manipulations, which you may implement in your query:

  • IPagedQuery (allows you to get a sample of data splitted by pages)
  • IFilteredQuery (allows to set collection of filters in request)
  • ISortedQuery (allows ASC/DESC ordering response data by definite property)
publicclassUserProfile{[JsonProperty("id")]publicGuidId{get;set;}[JsonProperty("firstName")]publicstringFirstName{get;set;}}

Query implementation:

publicclassGetProfilesQuery:IPagedQuery,IFilteredQuery,ISortedQuery{publicintPage{get;set;}=1;publicintPageSize{get;set;}=20;publicICollection<Filter>Filters{get;set;}publicSortSort{get;set;}}

Implement the following extension method for filtering:

publicstaticIEnumerable<Func<IQueryable<UserProfile>,IQueryable<UserProfile>>>CreateFilters(thisGetProfilesQueryquery){varfilters=newList<Func<IQueryable<UserProfile>,IQueryable<UserProfile>>>();varmap=typeof(UserProfile).GetProperties().Where(x =>x.GetCustomAttribute<JsonPropertyAttribute>()!=null).ToLookup(x =>x.GetCustomAttribute<JsonPropertyAttribute>().PropertyName);if(query.Filters!=null){foreach(varfilterinquery.Filters){varproperty=map[filter.PropertyName].FirstOrDefault();if(property==null){thrownewQueryException($"Could not find '{filter.PropertyName}' in model scheme.");}switch(property.Name){casenameof(UserProfile.FirstName):{filters.Add(x =>x.Where(e =>e.FirstName.Contains(filter.Value)));break;}
...}}}returnfilters;}

Implement the following extension method for sorting:

publicstaticFunc<IQueryable<UserProfile>,IQueryable<UserProfile>>CreateOrdering(thisGetProfilesQueryquery){Func<IQueryable<UserProfile>,IQueryable<UserProfile>>order=null;if(query.Sort!=null){varmap=typeof(UserProfile).GetProperties().Where(x =>x.GetCustomAttribute<JsonPropertyAttribute>()!=null).ToLookup(x =>x.GetCustomAttribute<JsonPropertyAttribute>().PropertyName);varproperty=map[query.Sort.PropertyName].FirstOrDefault();if(property==null){thrownewQueryException($"Could not find '{query.Sort.PropertyName}' in model scheme.");}Expression<Func<UserProfile,object>>sortExpression;switch(property.Name){casenameof(UserProfile.FirstName):{sortExpression= x =>x.FirstName;break;}
...}order=query.Sort.Order==SortOrder.Asc?(Func<IQueryable<UserProfile>,IQueryable<UserProfile>>)(x =>x.OrderBy(sortExpression)):(x =>x.OrderByDescending(sortExpression));}returnorder;}

Usage example:

varfilters=request.Query.CreateFilters();varordering=request.Query.CreateOrdering();varfilteredProfiles=newQuerySpecification<UserProfile>().WithFilters(filters).WithOrdering(ordering).ApplyFiltering(profiles);returnPageUtil.CreatePagedResults(filteredProfiles,request.Query.Page,request.Query.PageSize,Mapper.Map<UserProfile,ProfileResponse>);

About

This project is maintained by Softeq Development Corp. We specialize in .NET core applications.

Contributing

We welcome any contributions.

License

The Query Utils project is available for free use, as described by the LICENSE (MIT).

About

No description or website provided.

Topics

Resources

Stars

3 stars

Watchers

8 watching

Forks

Releases

Packages

Used by

Contributors

Languages