From the course: Building and Securing RESTful APIs in ASP.NET Core

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Build sort expressions dynamically

Build sort expressions dynamically - ASP.NET Core Tutorial

From the course: Building and Securing RESTful APIs in ASP.NET Core

Start my 1-month free trial

Build sort expressions dynamically

- [Instructor] Adding sort to a query with link is as simple as adding methods like query.orderby.thenbydescending, and so on. Unfortunately, this only works if you know the sort criteria at compile time. We need to apply different sort criteria depending on the query string of the request, which is a little trickier. To keep the sort options model light, we'll delegate this responsibility to sort options processor as well. And we'll simply, from this method, return processor.apply and deletgate this. Over in the sort options processor we'll need to add this apply method This will be a method that returns an Iqueryable of TEntity, and it also takes an IQueryable of TEntity. It's important to use IQueryable here and not INumerable. IQueryable contains an expression tree that can be transformed into a database query statement like sequel. By adding to that expression tree, we can dynamically change the query at runtime. Inside of the apply method, first we need to get all the valid…

Contents