Filtering results in ASP.NET Core Web API

asp.net core web api paging sorting filtering
web api filtering data
asp.net core api pagination
asp.net core mvc with ef core - sort, filter, paging
asp.net core paging sorting filtering
asp.net core authorization filter
paging in asp.net core 2.0 web api
asp net core rest api filtering

So I'm creating a database that allows users to create a "magic item" which they can then upload to my ASP.net Web API. This works just fine. And I want to show all these items in my web page by pulling them from the api. This also works fine. But now, when i try to filter, sort or limit the amount of searches, I still get the basic list of every item returned to me. Right now, there's only 14 entries, so no big deal, but i still want to get this done. But whatever i do, it always returns the full list.

This is the ASP.net Controller in visual studio:

[Route("api/v1/MagicItem")]
public class MagicItemController : Controller
{
    private ItemListContext context;

    public MagicItemController(ItemListContext context)
    {
        this.context = context;
    }

    [Produces("application/json")]
    [HttpGet]
    //public List<MagicItem> GetAllItems(string name, string category, string rarity, int? page, string sort, int limit = 5, string dir = "desc")
    public List<MagicItem> GetAllItems(
        string name, 
        string category, 
        string rarity, 
        int? page, 
        string sort,
        int limit = 5,
        string dir = "desc")
    {
        IQueryable<MagicItem> query = context.MagicItems;

        if (!string.IsNullOrWhiteSpace(name))
            query = query.Where(d => d.Name.Contains(name));
        if (!string.IsNullOrWhiteSpace(category))
            query = query.Where(d => d.Category == category);
        if (!string.IsNullOrWhiteSpace(rarity))
            query = query.Where(d => d.Rarity == rarity);


        if (!string.IsNullOrWhiteSpace(sort))
        {
            switch (sort)
            {
                case "Name":
                    if (dir == "asc")
                        query = query.OrderBy(d => d.Name);
                    else if (dir == "desc")
                        query = query.OrderByDescending(d => d.Name);
                    break;
                case "Rarity":
                    if (dir == "asc")
                        query = query.OrderBy(d => d.Rarity);
                    else if (dir == "desc")
                        query = query.OrderByDescending(d => d.Rarity);
                    break;
                case "Category":
                    if (dir == "asc")
                        query = query.OrderBy(d => d.Category);
                    else if (dir == "desc")
                        query = query.OrderByDescending(d => d.Category);
                    break;
            }
        }
        query = query.Take(limit);
        if (page.HasValue)
            query = query.Skip(page.Value * limit);

        return context.MagicItems.ToList();
    }
}

You are almost there:

just use:

return query.ToList();

instead of:

return context.MagicItems.ToList();

Filtering and paging collection result in ASP.NET Core Web API, Paging response data in ASP.NET Core WebApi projects. Paging a REST API response is equally important as filtering the data returned  Filtering and paging collection result in ASP.NET Core Web API Paging response data in ASP.NET Core WebApi projects Paging a REST API response is equally important as filtering the data returned through REST API endpoints and they quite often come together.

In a previous version of the code, i wasn't using paging yet, so i created the list of items in the return-line itself. All i had to do was actually return the query i was working on.

Filters in ASP.NET Core, https://docs.microsoft.com › aspnet › core › data › ef-mvc › sort-filter-page In this article, we will cover filtering in ASP.NET Core Web API. We’ll learn what filtering is, how it’s different from searching, and how to implement it in a real-world project. While not critical as paging , filtering is still an important part of a flexible REST API, so we need to know how to implement it in our API projects.

Create method for filter: First, we convert filter codes to a string

             string where =string.Empty;

            if (!string.IsNullOrWhiteSpace(name))
                      where + = name;
           if (!string.IsNullOrWhiteSpace(category))
                      where += category;
           if (!string.IsNullOrWhiteSpace(rarity))
                        where += rarity;

               var entity = 
                    setFilter(context.MagicItems,where,order)

                Return entity;

Your method:

          Public IEnumerable setFilter(TEntity entity 
                   ,func<IQueryable<bool 
                   out,TEntity)> where 
                        =null , func<IQueryable<TEntity> 
                  ,IOrderedQueryable<TEntity>> order =null)
                 {
                      IQueryable query = entity;
                      If(whrer != null)
                        {
                           query =query.where(where);
                         }
                          If(order != null)
                        {
                           query =Order(query);
                         }

                        Return query.toList();
                  }

Tutorial: Add sorting, filtering, and paging, You are almost there: just use: return query.ToList();. instead of: return context.​MagicItems.ToList();. This is another post in a series on creating performant and scalable web APIs using ASP.NET Core. In this post, we’ll continue to focus on operating on large collections of data – this time allowing the consumers of our API to filter and search on the collection …. The reasons are the same as paging from the last post:

Filtering results in ASP.NET Core Web API, In this post, we'll continue to focus on operating on large collections of data - this time allowing the consumers of our API to filter and search on  Problem. How to implement filtering in ASP.NET Core Web API. Solution. To an empty project update Startup class to add services and middleware for MVC,

Scalable and Performant ASP.NET Core Web APIs: Filtering and , How to implement filtering in ASP.NET Core Web API. Solution. To an empty project, update Startup class to add services and middleware for  Download source code from Github; Problem. How to implement filtering in ASP.NET Core Web API. Solution. To an empty project, update Startup class to add services and middleware for MVC:

Filtering in ASP.NET Core 2.0 Web API, Web API includes filters to add extra logic before or after action method executes. Filters can be used to provide cross-cutting features such as logging, exception  How to efficiency filter result set in a RestApi with ASP.net Core How would you implement filtering/paging in your ASP.NET core WebApi? asp.net-web-api

Comments
  • Stefan pretty much answered your question. This one might help you shorten the code. More usage here
  • There are few open source projects that implement this. For example sieve.
  • I had just noticed. I make these kinds of stupid mistakes far too often.
  • Get some sleep, it helps ;-)