MikesSifter 2.0.1

dotnet add package MikesSifter --version 2.0.1
                    
NuGet\Install-Package MikesSifter -Version 2.0.1
                    
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="MikesSifter" Version="2.0.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="MikesSifter" Version="2.0.1" />
                    
Directory.Packages.props
<PackageReference Include="MikesSifter" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add MikesSifter --version 2.0.1
                    
#r "nuget: MikesSifter, 2.0.1"
                    
#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.
#:package MikesSifter@2.0.1
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=MikesSifter&version=2.0.1
                    
Install as a Cake Addin
#tool nuget:?package=MikesSifter&version=2.0.1
                    
Install as a Cake Tool

MikesSifter

MikesSifter is a versatile and extensible library designed to provide powerful filtering, sorting and paging capabilities in .NET applications.

Installation

Minimum Requirements: .NET 8.0.x

Download from NuGet

Package Manager
NuGet\Install-Package MikesSifter -Version *version_number*
.NET CLI
dotnet add package MikesSifter --version *version_number*

Usage for ASP.NET Core WebApi

In this example, consider an app with a User entity that can have many projects. We'll use MikesSifter to add sorting, filtering and pagination capabilities when retrieving all available users.

1. Define the application sifter.

Inherit the base implementation MikesSifter and override the Configure method.

public class ApplicationSifter : MikesSifter
{
    protected override void Configure(MikesSifterBuilder builder)
    {
        // Here apply your configurations.
    }
}

2. Configure the properties you want to sort/filter in your models.

2.1. Using a separate entity configuration class.
public class UserSifterConfiguration : IMikesSifterEntityConfiguration<User>
{
    public void Configure(MikesSifterEntityBuilder<User> builder)
    {
        builder
            .Property(e => e.FullName)
            .EnableFiltering()
            .EnableSorting();

        builder
            .Property(e => e.Gender)
            .EnableSorting()
            .EnableFiltering();

        builder
            .Property(e => e.BirthDate)
            .EnableFiltering()
            .EnableSorting();

        builder
            .Property(e => e.Projects)
            .EnableFiltering()
            .HasCustomFilters(e =>
            {
                e.WithFilter(FilteringOperators.Contains, filterValue =>
                {
                    ArgumentException.ThrowIfNullOrWhiteSpace(filterValue);
                    return u => u.Projects.Any(o => o.Id == Guid.Parse(filterValue));
                });
            });

        builder
            .Property(e => e.Passport.Number)
            .EnableFiltering()
            .EnableSorting()
            .HasAlias("user_passportNumber");
    }
}

Apply particular configuration by calling ApplyConfiguration<T>:


protected override void Configure(MikesSifterBuilder builder)
{
     builder.ApplyConfiguration<UserSifterConfiguration>();
}

Apply configurations from a particular assembly by calling ApplyConfigurationsFromAssembly:

protected override void Configure(MikesSifterBuilder builder)
{
     builder.ApplyConfigurationsFromAssembly(Assembly.GetExecutingAssembly());
}
2.2. Using Fluent API without defining a separate configuration class.

protected override void Configure(MikesSifterBuilder builder)
{
     builder.Entity<User>(e =>
     {
          e.Property(i => i.FullName)
              .EnableFiltering()
              .EnableSorting();

          e.Property(i => i.Gender)
              .EnableSorting()
              .EnableFiltering();

          e.Property(i => i.BirthDate)
              .EnableFiltering()
              .EnableSorting();

          e.Property(e => e.Projects)
              .EnableFiltering()
              .HasCustomFilters(e =>
              {
                  e.WithFilter(FilteringOperators.Contains, filterValue =>
                  {
                      ArgumentException.ThrowIfNullOrWhiteSpace(filterValue);
                      return u => u.Projects.Any(o => o.Id == Guid.Parse(filterValue));
                  });
              });

          e.Property(i => i.Passport.Number)
              .EnableFiltering()
              .EnableSorting()
              .HasAlias("user_passportNumber");
     });
}

3. Implement IMikesSifterModel.

In our example, we will use a custom model implementation as the POST body. However, you can implement your own using, for example, GET method with query parameters.

public sealed class ApplicationSifterModel : IMikesSifterModel
{
    public FilteringOptions? FilteringOptions { get; init; }
    
    public SortingOptions? SortingOptions { get; init; }
    
    public PagingOptions? PagingOptions { get; init; }

    public FilteringOptions? GetFilteringOptions() => FilteringOptions;

    public SortingOptions? GetSortingOptions() => SortingOptions;

    public PagingOptions? GetPagingOptions() => PagingOptions;
}

4. Dependency Injection (DI).

Add the application sifter to the services by calling the AddSifter extension method.

builder.Services.AddSifter<ApplicationSifter>();

5. Let's use.

Inject IMikesSifter into controller to use the sifter capabilities.

5.1. Apply filtering, sorting and paging all together by calling the Apply method.
[HttpPost("full")]
public IActionResult Full(ApplicationSifterModel model)
{
    var result = sifter.Apply(dbContext
        .Users
        .Include(e => e.Projects)
        .Include(e => e.Passport), model);
    
    return Ok(result.Select(e => e.ToViewModel()).ToList());
}
5.2. Apply only filtering by calling the ApplyFiltering method.
[HttpPost("filtering")]
public IActionResult OnlyFiltering(FilteringOptions filteringOptions)
{
    var result = sifter.ApplyFiltering(dbContext
        .Users
        .Include(e => e.Projects)
        .Include(e => e.Passport), filteringOptions);
    
    return Ok(result.Select(e => e.ToViewModel()).ToList());
}
5.3. Apply only sorting by calling the ApplySorting method.
[HttpPost("sorting")]
public IActionResult OnlySorting(SortingOptions sortingOptions)
{
    var result = sifter.ApplySorting(dbContext
        .Users
        .Include(e => e.Projects)
        .Include(e => e.Passport), sortingOptions);
    
    return Ok(result.Select(e => e.ToViewModel()).ToList());
}
5.4. Apply only paging by calling the ApplyPaging method.
[HttpPost("paging")]
public IActionResult OnlyPaging(PagingOptions pagingOptions)
{
    var result = sifter.ApplyPaging(dbContext
        .Users
        .Include(e => e.Projects)
        .Include(e => e.Passport), pagingOptions);
    
    return Ok(result.Select(e => e.ToViewModel()).ToList());
}
Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  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.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
2.0.1 180 1/11/2025
2.0.0 221 8/31/2024
1.0.0 197 7/22/2024