Skip to content

Filter Operations

Furkan Güngör edited this page Dec 3, 2021 · 3 revisions

Summary

EasyRepository contains two different methods for filter operations.

Based on your AutoFilterer Dto (Data Transfer Object) classes, it performs a dynamic query on the related entity.

Among the default features of AutoFilterer;

  • Pagination
  • Sorting
  • Range (min-max)
  • Contains

operations such as.

This repo contains limited information about AutoFilterer. Visit the Auto Filterer Wiki for more information.

Manual Filter (Classic Where Expression)

public class MyClass
{
    private readonly IRepository repository;
    public MyClass(IRepository repository)
    {
        this.repository = repository;
    }

    public async Task<List<Author>> ClassicWhere()
    {
        var entity = await repository.GetMultipleAsync<Author>(false, whereExpression: a => a.IsDeleted == false);

        // or

        var entity2 = await repository.GetMultipleAsync<Author>(false, whereExpression: a => a.IsDeleted == false && a.Name.Contains("t"));

        return entity
    }
}

With AutoFilterer

Step 1 : Must Be Create Filter Data Transfer Object

[PossibleSortings("Name", "Surname", "CreationDate", "ModificationDate", "DeletionDate")]
public class AuthorFilterDto : PaginationFilterBase
{
    public AuthorFilterDto()
    {
        this.Sort = "Name";
        this.SortBy = AutoFilterer.Enums.Sorting.Descending;
    }
    [ToLowerContainsComparison]
    public string Name { get; set; }
    [ToLowerContainsComparison]
    public string Surname { get; set; }
    public Range<DateTime> CreationDate { get; set; }
    public Range<DateTime> ModificationDate { get; set; }
    public Range<DateTime> DeletionDate { get; set; }
}

Step 2 : Specify the filter object in the GetMultiple endpoint.

public class MyClass
{
    private readonly IRepository repository;
    public MyClass(IRepository repository)
    {
        this.repository = repository;
    }

    public async Task<List<Author>> AutoFilterer(AuthorFilterDto dto)
    {
        var entities = await repository.GetMultipleAsync<Author, AuthorFilterDto>(asNoTracking: false, dto);

        return entities;
    }
}

Step 3 : Sit back and sip your coffee ☕😇

AutoFilterer With Include Expression

public class MyClass
{
    private readonly IRepository repository;
    public MyClass(IRepository repository)
    {
        this.repository = repository;
    }

    public async Task<List<Author>> AutoFiltererWithInclude(AuthorFilterDto dto)
    {
        Func<IQueryable<Author>, IIncludableQueryable<Author, object>> include = a => a.Include(i => i.Books);
        var entity = await repository.GetMultipleAsync<Author, AuthorFilterDto>(asNoTracking: false, dto, includeExpression: include);

        return entity;
    }
}

AutoFilterer With Select Expression

public class MyClass
{
    private readonly IRepository repository;
    public MyClass(IRepository repository)
    {
        this.repository = repository;
    }

    public async Task<List<object>> AutoFiltererWithSelect(AuthorFilterDto dto)
    {
        var entity = await repository.GetMultipleAsync<Author, AuthorFilterDto, object>(asNoTracking: false, dto, select => new
        {
            SelectName = select.Name,
            SelectDate = select.CreationDate
        });

        return entity;
    }
}

AutoFilterer With Select And Include Expression

public class MyClass
{
    private readonly IRepository repository;
    public MyClass(IRepository repository)
    {
        this.repository = repository;
    }

    public async Task<List<object>> AutoFiltererWithIncludeAndSelect(AuthorFilterDto dto)
    {
         Func<IQueryable<Author>, IIncludableQueryable<Author, object>> include = a => a.Include(i => i.Books);
         var entities = await repository.GetMultipleAsync<Author, AuthorFilterDto, object>(false, dto, select => new
         {
             SelectName = select.Name,
             SelectDate = select.CreationDate
         }, include);

        return entity;
    }
}