简体   繁体   English

在 ASP.NET Core Web API 中过滤结果

[英]Filtering results in ASP.NET Core Web API

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.所以我正在创建一个数据库,允许用户创建一个“魔法项目”,然后他们可以将其上传到我的 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.我想通过从 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.现在,只有 14 个条目,所以没什么大不了的,但我仍然想完成这项工作。 But whatever i do, it always returns the full list.但无论我做什么,它总是返回完整列表。

This is the ASP.net Controller in visual studio:这是 Visual Studio 中的 ASP.net 控制器:

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

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.我所要做的就是返回我正在处理的查询。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM