romaklayt.DynamicFilter.Common 1.8.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package romaklayt.DynamicFilter.Common --version 1.8.0
NuGet\Install-Package romaklayt.DynamicFilter.Common -Version 1.8.0
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="romaklayt.DynamicFilter.Common" Version="1.8.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add romaklayt.DynamicFilter.Common --version 1.8.0
#r "nuget: romaklayt.DynamicFilter.Common, 1.8.0"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install romaklayt.DynamicFilter.Common as a Cake Addin
#addin nuget:?package=romaklayt.DynamicFilter.Common&version=1.8.0

// Install romaklayt.DynamicFilter.Common as a Cake Tool
#tool nuget:?package=romaklayt.DynamicFilter.Common&version=1.8.0

romaklayt.DynamicFilter

Simple filter for .net WebApi that enables your endpoint to query your requests by url.

It provides ways to query, order and page your webapi and mvc.

.Net Core and .Net

First, download the packages into your project from nuget

nuget install romaklayt.DynamicFilter.Extensions
nuget install romaklayt.DynamicFilter.Binder.Net

and, if you need, add to your ConfigureServices Startup.cs class for register value provider (JSON) for body JSON POST request support (providers for form-data, x-www-form-urlencoded work them by default)

services.AddControllers(options => options.ValueProviderFactories.Add(new JsonBodyValueProviderFactory()));

.Net Framework (>=4.6.1)

First, download the packages into your project from nuget

nuget install romaklayt.DynamicFilter.Extensions

If you need to add a filter to the web api controller use the package

nuget install romaklayt.DynamicFilter.Binder.NetFramework.WebApi

and, if you need, add to your WebApiConfig class for register value providers (form-data, x-www-form-urlencoded and JSON) for body POST request

config.AddDynamicFilterProviders();

If you need to add a filter to the MVC controller use the package

nuget install romaklayt.DynamicFilter.Binder.NetFramework.Mvc

and, if you need, add to your Application_Start Global.asax.cs class for register value providers (form-data, x-www-form-urlencoded and JSON) for body POST request

DynamicFilterProviders.AddProviders();

If you need to use DynamicFilter with IAsyncEnumerable or IAsyncQueryable use the package

nuget install romaklayt.DynamicFilter.Extensions.Async

After downloaded, go to your webapi and create an get or post endpoint receiving DynamicComplexModel as parameter.

[HttpGet]
public Task<List<User>> Get(DynamicComplexModel filter)

Now you can query your endpoint with the DynamicFilter properties. Check "tests" folder on the Api projects for examples.

[HttpGet]
public Task<List<User>> Get(DynamicComplexModel filter)
{
    var result = this.users.UseFilter(filter);

    return Task.FromResult(result.ToList());
}

DynamicFilter.Parser will transform your URI Queries into .Net Expressions. That way, you can use these expressions to filter your values into your database repository.

Simple Filter

You can use a DynamicComplexModel model for filtering. A DynamicFilterModel model without additional properties is also available.

public Task<List<User>> Get(DynamicComplexModel filter) #or DynamicFilterModel
{
    var result = users.UseFilter(filter);

    return Task.FromResult(result.ToList());
}

Example Uri:

GET http://url?filter=name=Bruno

Expression generated by Uri:

x => x.Name == "Bruno"

Filters are also supported for nested collections.

GET http://url?filter=roles.name=admin

Your request will be converted to:

users.Where(x => x.Roles.Any(y => y.Name == "admin"))

Complex Filter

Example Uri:

GET http://url?filter=name=Bruno,lastname%r,age>=27

Expression generated by Uri:

x => x.Name == "Bruno" 
  && x.LastName.ToLower().Contains("r") 
  && x.Age >= 27

You can add conditions to your filter sorting your filters with a comma (,), as shown above.

Nested Filer

Example Uri:

GET http://url?filter=address.number=23

Expression generated by Uri:

x => x.Address.Number == 23

Multiple filter values for a single property

Such a query will select all entities whose age corresponds to 23, 28 or 27 years

GET http://url?filter=age=[23|28|37] #

To recognize the query, you need to put the values in square brackets [ and ]. The values inside the brackets must be separated by '|' (which will be equivalent to or) or '~' (which will be equivalent to and). Comparison operators are available the same as by default.

Ordering

You can also order your queries via DynamicFilter. You simply need to add an order parameter on your query, where you specify the property you'll use for order.

GET http://url?filter=name=Bruno&order=name

Default order type is Ascending. You can specify the order type with an equals (=) Asc or Desc after the property.

GET http://url?filter=name=Bruno&order=name=Asc

GET http://url?filter=name=Bruno&order=name=Desc

To sort by multiple properties:

GET http://url?filter=name=Bruno&order=name=asc,firstname=asc

GET http://url?filter=name=Bruno&order=name=desc,firstname

If you do not specify the sort type, asc is used by default.

On your DynamicFilter object received on the endpoint, you'll get the orderType as an Enum, this way you can order by the type specified on enum.

public Task<List<User>> Get(DynamicComplexModel filter)
{
    var result = users.UseFilter(filter);

    return Task.FromResult(result.ToList());
}

Pagging

GET http://url?filter=name%b&order=name&page=1&pagesize=10

In romaklayt.DynamicFilter.Extensions and romaklayt.DynamicFilter.Extensions.Async there is a method of expanding the ** ToPagedList** which returns PageModel with info about page and your filtered data.

var page = users.ToPagedList(filterModel);

If you no need page info, you simply needs to add the parameters page and pagesize on your get request.

result = result.UseFilter(filter); #page mode without info

If you specify the page number and size in the filter model, pagination is disabled when using UseFilter. You can also disable pagination by forcibly calling:

result = result.UseFilter(filter, false); #only filtering without pagination

Example:

[HttpGet("page")]
public async Task<PageModel<User>> GetPage(DynamicComplexModel filterModel)
{
    var filteredUsers = await Data.Users.UseFilter(filterModel);
    return await filteredUsers.ToPagedList(filterModel);
}

Select

To select, you simply needs to add the parameter select with the properties you want to select from. It will render either an linq select and a plain string select.

GET http://url?select=name,age

If you select a nested property, from default, the properties from the root model will be ignored, to select all the root properties, use the word root.

GET http://url?select=address.zip,root #select Address.Zip and all root properties

If you need to select properties from only one model (the GetById method, for example), you can also use the method ** UseFilter**.

[HttpGet("{id}")]
public async Task<object> GetById(DynamicSelectModel dynamicSelectModel, Guid id)
{
    var user = await Data.Users.UseSelect(dynamicSelectModel).FirstOrDefaultAsync(user => user.Id == id);
    return user;
}

Filter operator support

  • Equals (=)
GET http://url?filter=name=Bruno
  • Contains Invariant Case (%)
GET http://url?filter=name%b
  • Contains Case Sensitive (%%)
GET http://url?filter=name%%B
  • GreaterThan (>)
GET http://url?filter=age>15
  • GreaterOrEqual (>=)
GET http://url?filter=age>=15
  • LessThan (<)
GET http://url?filter=age<15
  • LessOrEqual (<=)
GET http://url?filter=age<=15
  • NotEquals (!=)
GET http://url?filter=age!=15
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 is compatible. 
.NET Framework net461 is compatible.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (4)

Showing the top 4 NuGet packages that depend on romaklayt.DynamicFilter.Common:

Package Downloads
romaklayt.DynamicFilter.Parser

Library for using a dynamic filter with Linq

romaklayt.DynamicFilter.Binder.Net

Simple binding that enables dynamic filtering, select, paging, order on your .Net and .Net Core webapi and mvc controllers

romaklayt.DynamicFilter.Binder.NetFramework.Mvc

Simple binding that enables dynamic filtering, select, paging, order on your .Net Framework Mvc controller

romaklayt.DynamicFilter.Binder.NetFramework.WebApi

Simple binding that enables dynamic filtering, select, paging, order on your .Net Framework Web Api controller

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
2.5.7 106 4/27/2024
2.5.6 105 4/18/2024
2.5.5 115 4/10/2024
2.5.4 116 4/2/2024
2.5.3 110 4/1/2024
2.5.2 180 3/8/2024
2.5.1 202 3/3/2024
2.5.0 356 1/6/2024
2.4.5 567 8/14/2023
2.4.4 527 8/14/2023
2.4.3 722 6/18/2023
2.4.2 596 6/18/2023
2.4.1 647 6/7/2023
2.4.0 609 6/5/2023
2.3.5 761 6/2/2023
2.3.4 894 4/19/2023
2.3.3 901 4/18/2023
2.3.2 936 4/16/2023
2.3.1 1,041 4/9/2023
2.3.0 1,020 4/9/2023
2.2.1 914 4/8/2023
2.2.0 940 4/8/2023
2.2.0-tags-2-2-0-beta-1.1 75 4/8/2023
2.1.2 1,616 11/4/2022
2.1.1 1,598 11/4/2022
2.1.0 1,761 9/24/2022
2.1.0-beta.1 95 9/9/2022
2.0.0 1,865 9/2/2022
2.0.0-beta.3 121 7/28/2022
2.0.0-beta.2 102 7/26/2022
2.0.0-beta.1 114 7/19/2022
1.9.3 1,922 4/21/2022
1.9.2 1,956 4/10/2022
1.9.1 1,890 4/6/2022
1.9.0 1,936 4/5/2022
1.8.1 1,897 3/27/2022
1.8.0 1,963 3/7/2022
1.7.12 1,787 10/5/2022
1.7.11 1,975 2/8/2022
1.7.10 1,922 2/8/2022
1.7.9 1,955 1/11/2022
1.7.8 1,995 1/11/2022
1.7.7 1,935 1/11/2022
1.7.6 2,004 1/11/2022
1.7.5 1,814 1/10/2022
1.7.4 1,181 12/26/2021
1.7.3 1,195 12/17/2021
1.7.2 1,185 12/12/2021
1.7.1 1,181 12/3/2021
1.7.0 1,281 11/14/2021
1.6.5 1,420 11/6/2021
1.6.4 1,450 10/26/2021
1.6.3 1,394 10/12/2021
1.6.2 1,399 10/12/2021
1.6.1 1,346 10/12/2021
1.6.0 1,385 10/11/2021
1.5.5 1,375 10/6/2021
1.5.4 1,360 10/6/2021
1.5.3 1,415 9/27/2021
1.5.2 1,340 9/27/2021
1.5.1 1,379 9/27/2021
1.5.0 1,362 9/27/2021
1.4.1 1,390 9/24/2021
1.4.0 1,396 9/24/2021
1.3.1 1,362 9/22/2021
1.3.0 1,302 9/20/2021
1.2.0 1,341 9/17/2021
1.1.4 1,450 9/17/2021
1.1.3 1,424 9/17/2021
1.1.2 1,344 9/17/2021
1.1.1 1,403 9/16/2021
1.1.0 1,419 9/13/2021
1.0.2 1,476 9/10/2021
1.0.1 1,500 9/10/2021
1.0.0 1,386 9/10/2021