Making query from the client side is an antipattern, but sometimes your project is a CRUD and you know that making a client-side query doesn't make a mess to your project, for these type of situations you can use DynamicQuery.Net.
$ Install-Package DynamicQuery.Net
$ dotnet add package DynamicQuery.Net
Creating FilterInput object:
var filerInput = new FilterInput
{
Operation = OperationTypeEnum.GreaterThan,
Property = "Date",
Type = InputTypeEnum.String,
Value = "2017/04/08"
};
if you want to filter more than one field , you can create a List of FilterInput objects
var filerInput = new List<FilterInput>
{
new FilterInput
{
Operation = OperationTypeEnum.GreaterThan,
Property = "Date",
Type = InputTypeEnum.String,
Value = "2017/04/08"
}
};
if you want to have more than one value for a single field, you can feed Value with a List:
var filerInput = new List<FilterInput>
{
new FilterInput
{
Operation = OperationTypeEnum.NotEqual,
Property = "ClassNo",
Type = InputTypeEnum.Number,
Value = new List<object>{2,3,4}
}
};
Using FilterInput object
Now we can use our filterInput variable:
myQueryable = myQueryable.Filter(filerInput);
Creating OrderInput object:
For a single field:
var orderItem = new OrderInput {Order = OrderTypeEnum.Desc, Property = "Date"};
For a List of fields:
var orderInput = new List<OrderInput>
{
new OrderInput {Order = OrderTypeEnum.Desc, Property = "Date"},
new OrderInput {Order = OrderTypeEnum.Asc, Property = "Name"},
new OrderInput {Order = OrderTypeEnum.Asc, Property = "ID"}
};
Using OrderInput object:
myQueryable = myQueryable.Order(orderInput);
Creating and Using OrderFilterInput object
var orderFilterInput = new OrderFilterInput
{
Filter = filerInput,
Order = orderInput
}
myQueryable = myQueryable.Filter(orderFilterInput);
If you want to send some objects to the server that you won't use it as a Filter in IQueryable, you can use NonFilter Dictionary.
var nonFilterInput = new Dictionary<string, string>
{
{"TestName1", "TestValue1"},
{"TestName2", "TestValue2"},
{"TestName3", "TestValue3"}
};
var orderFilterNonFilterInput = new OrderFilterNonFilterInput()
{
Order = orderInput,
Filter = filterInput,
NonFilter = nonFilterInput
};
myQueryable = myQueryable.Filter(orderFilterNonFilterInput);
If you want to use paging in your filtering you can use PagingInput object :
var paging = new PagingInput
{
Page = 2,
Size = 10
};
myQueryable = myQueryable.Paging(paging);
All of the above-mentioned capabilities can be achieved by using a DynamicQueryNetInput object as a parameter to the Filter() extension method:
var dynamicQueryNetInput = new DynamicQueryNetInput()
{
Order = orderInput,
Filter = filterInput,
NonFilter = nonFilterInput,
Paging = paging
};
myQueryable = myQueryable.Filter(dynamicQueryNetInput);
In the client side send a JSON to the server:
{
"Filter":[{"Property":"ContactNumber" , "Value":[2,3,4] , "Type":"Number" , "Operation":"Equal"}],
"Order":[{"Property":"Date" , "Order":"Desc"}],
"NonFilter":{"Calculate":"True"},
"Paging":{"Page":3 , "Size":10}
}
In the server just use it in .Filter() Method:
public HttpResponseMessage Filter(DynamicQueryNetInput dynamicQueryNetInput)
{
return Request.CreateResponse(HttpStatusCode.OK, myQueryable.Filter(dynamicQueryNetInput));
}