SmartWhere is a method that aims to make the Queryable.Where
method smarter and is based on the foundations of .NET.
I would be very happy if you could star ⭐ the project.
The usage of SmartWhere is quite simple.
- Install
SmartWhere
NuGet package from here.
PM> Install-Package SmartWhere
- You need to define our Request object for SmartWhere and sign it with the
IWhereClause
interface.
public class PublisherSearchRequest : IWhereClause
- We mark the properties to be included in the filter with the
WhereClause
Attribute.
public class PublisherSearchRequest : IWhereClause
{
[WhereClause]
public int Id { get; set; }
[WhereClause(PropertyName = "Name"]
public string PublisherName { get; set; }
[WhereClause("Book.Name")]
public string BookName { get; set; }
[WhereClause("Books.Author.Name")]
public string AuthorName { get; set; }
}
- And we filter smartly. That's it.
[HttpPost]
public IActionResult GetPublishers(PublisherSearchRequest request)
{
var result = _context.Set<Publisher>()
.Include(x => x.Books)
.ThenInclude(x => x.Author)
.Where(request)
.ToList();
return Ok(result);
}
Be sure to check out the Wiki page for more details. ⭐