The idea behind quryable builder is simple - to reduce the amount of boilerplate query code in your codebase. It is designed around the idea of query object and tries to further automate the process. You should simply design a class that will serve as a query object, decorate it with some of the predifined attributes, and, if needed, inherit from our base clasess and let the builder to the heavy lifting of query composition.
In it's essence, it is a collection of extension methods for predicate building, sorting and paging on top of generic IQueryable interface. Based on your use-case, there are several options to choose from.
Nuget Package Doublel.QueryableBuilder
Install-Package Doublel.QueryableBuilderOR
dotnet add package Doublel.QueryableBuilderMinimum Requirements: .NET Standard 2.0
The tool was initialy designed for company's internal purposes, but we've figured out it could help someone else as well. If the example clicks for you, proceed to building some queryes.
Let's say you have a user storage you would like to query and it's exposed via the IQueryable interface.
First, you have your user class:
publicclassTestUser{publicintId{get;set;}publicstringFirstName{get;set;}publicstringUsername{get;set;}publicintAge{get;set;}}Then, you should define your query object class:
publicclassUserQuery{[QueryProperty]publicint?Age{get;set;}[QueryProperties(ComparisonOperator.Contains,"FirstName","Username")]publicstringKeyword{get;set;}}And finally, you query your datasource:
varqueryObject=newUserQuery{Age=20;Keyword="ma"}varusers=newList<User>{/* a bunch of users */}.AsQueryable();varresult=users.BuildQuery(queryObject);The resulting dataset will consist of all the users having being exactly 20 years old and containing string "ma" (case-insensitive) in either their Username of FirstName properties.