3

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();
    }
}
2
  • Stefan pretty much answered your question. This one might help you shorten the code. More usage here Commented Jun 2, 2018 at 14:01
  • There are few open source projects that implement this. For example sieve. Commented Jun 2, 2018 at 16:05

3 Answers 3

3

You are almost there:

just use:

return query.ToList();

instead of:

return context.MagicItems.ToList();
Sign up to request clarification or add additional context in comments.

2 Comments

I had just noticed. I make these kinds of stupid mistakes far too often.
Get some sleep, it helps ;-)
0

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.

Comments

0

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();
                  }

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.