I have an ASP .Net Core 2.1 Web Api. I was wondering if there is an "elegant solution" for constructing filters on my DbContext based on query strings? So... say I have one [optional] query string:
// GET: api/Accounts
[HttpGet]
public IEnumerable<File> GetAccount([FromQuery] bool? isActive)
{
if (isActive.HasValue)
return _context.Accounts.Where(a => a.IsActive == isActive.Value);
else
return _context.Accounts;
}
Simple enough... But say I have a number of (optional) query strings:
// GET: api/Accounts
[HttpGet]
public IEnumerable<File> GetAccount([FromQuery] bool? isActive, [FromQuery] string type, [FromQuery] int? agentId, [FromQuery] bool? someOtherFilter)
{
}
As you can see, constructing the filters now gets harder, because there can be a combination of filters, depending which ones have been supplied. I could check if the first query string has a value, if it does, perform the filter and save the results into a temp variable. Then I could check the next query string, if that has a value, then perform that filter on the temp variable, and so on and so on. But that sounds like it's going to be slow... Any other suggestions? Thanks...