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

时间:2018-06-02 13:52:49

标签: c# asp.net-core

所以我正在创建一个数据库,允许用户创建一个“魔术项目”,然后他们可以上传到我的ASP.net Web API。这很好用。我想通过从api中提取所有这些项目来显示我的网页。这也很好。但是现在,当我尝试过滤,排序或限制搜索量时,我仍然可以获得返回给我的每个项目的基本列表。现在,只有14个条目,所以没什么大不了的,但我仍然想要完成这个。但无论我做什么,它总会返回完整列表。

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

3 个答案:

答案 0 :(得分:3)

你快到了:

只需使用:

return query.ToList();

而不是:

return context.MagicItems.ToList();

答案 1 :(得分:0)

在以前版本的代码中,我还没有使用分页,所以我在返回行中创建了项目列表。我所要做的就是返回我正在处理的查询。

答案 2 :(得分:0)

创建过滤器的方法:  首先,我们将过滤器代码转换为字符串

             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;

你的方法:

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