2

I'm in the process of developing an ASP.NET Core WebAPI that uses EntityFramework to access the datastore. One of the things we need to support is sorting and filtering.

I'm assuming that the HTTP query-string will be populated with the various criteria for the filtering and sorting. But I'd like to see some actual real-world examples of fairly complex filtering/sorting functions, how the filter and sort objects are constructed, and how the EF queries are built-up to obtain the data from the database.

I've done quite a bit of searching on the topic but all I can find are trivial examples which wouldn't be appropriate for a complex, enterprise-level application.

Lastly, I'm not interested in OData. This needs to be a standard REST API.

3
  • Please define "standard REST API".
    – MetaFight
    Commented Dec 30, 2016 at 22:45
  • i.e. not the OData protocol. Commented Dec 31, 2016 at 0:31
  • Hand rolling your own query and filtering system is, by definition, going to make your REST API non-standard. OData, on the other hand, is a standard. You probably mean "REST compliant" as Robert Harvey has mentioned.
    – MetaFight
    Commented Dec 31, 2016 at 16:01

1 Answer 1

2

This blog post illustrates suitable REST-compliant query strings, in particular:

?sort=created,modified

and

?page=2&page_entries=10

To create custom queries, you can either build up a custom SQL String and submit it to the database using the EF method ExecuteQuery(), or use Linq to add the necessary Where() or OrderBy() conditions.

If you use Linq, you can either selectively add clauses to an IQueryable using if statements, use Dynamic Linq if you want to deal with the query strings directly, or use Predicate Builder to build a custom condition predicate.

Further Reading
Sorting, Filtering, and Paging with the Entity Framework in an ASP.NET MVC Application

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.