AutoFilterer icon indicating copy to clipboard operation
AutoFilterer copied to clipboard

Select Expression Support

Open enisn opened this issue 4 years ago • 2 comments
trafficstars

Summary

This feature aims to generate an expression for .Select() Linq method.

Declaration

See usage example for better understanding:

  • Entity
public class Book
{
  public string Name { get; set; }

  public int TotalPage { get; set; }

  public DateTime CreateDate { get; set; }

  public virtual ICollection<BookOrder> BookOrders { get; set; }
}
  • FilterDto (As always but inherits SelectorFilterBase)
public class BookFilterDto : SelectorFilterBase<BookDto>
{
  [ToLowerContainsComparison]
  public string Name { get; set; }

  public Range<int> TotalPage { get; set; }

  public Range<DateTime> CreateDate { get; set; }
}
  • Output dto:
public class BookDto
{
  [From("Name")]
  public string Title { get; set; }

  public int TotalPage { get; set; }

  public DateTime CreateDate { get; set; }

  [Count("BookOrders")]
  public int SellCount { get; set; }

  [Sum("BookOrders.Price")]
  public float TotalSoldPrice { get; set; }
}

Usage

As default, SelectorFilterBase should override ApplyFilter() method, so calling ApplyFilter method will work.

[HttpGet]
public IActionResult GetBooks([FromQuery] BookFilterDto filter)
{
    var query = db.Books.ApplyFilter(filter);  // IQueryable<BookDto>
    return Ok(query.ToList());
}
  • Or alternative usage:

As default, SelectorFilterBase should override ApplyFilter() method, so calling ApplyFilter method will work.

[HttpGet]
public IActionResult GetBooks([FromQuery] BookFilterDto filter)
{
    var query = db.Books.ApplySelector(filter)  // IQueryable<BookDto>
                            .Where(x => x.TotalSoldPrice > 299.95); // If db provider supports.

    return Ok(query.ToList());
}

Structure

  • FilterBase
    • OrderableFilterBase
      • PaginationFilterBase
        • SelectorFilterBase<T>

TODO List

  • [x] Extension methods for last user usage
  • [ ] SelectorFilterBase and SelectorPaginationFilterBase implementation.
  • [ ] Attributes
    • [ ] Sum
    • [ ] Count
    • [ ] Min
    • [ ] Max
    • [ ] Average

enisn avatar Dec 22 '20 20:12 enisn

This might be useful: https://stackoverflow.com/questions/12701737/expression-to-create-an-instance-with-object-initializer/12705338#12705338

enisn avatar Dec 25 '20 06:12 enisn

Dynamic Select is not compatible with Open API 3.0 standards. This is the main purpose of AutoFilterer. Only static select support can be provided.

  • Also, this is a 'Filtering' library, it doesn't aim projection by the way

enisn avatar Jan 11 '22 05:01 enisn