【问题标题】:How to use 'Any' in AsQueryable methods?如何在 AsQueryable 方法中使用“任何”?
【发布时间】:2014-04-22 15:44:51
【问题描述】:

如果给定任何参数“名称”和“价格”或仅名称,我正在尝试让我的查询输出所有数据。我尝试使用 'Any' 参数,但它给了我 -- 无法将类型 'bool' 隐式转换为 -- 错误。

我之前使用的是“FirstOrDefault”,但这只是输出第一条记录的详细信息。

我可以使用或研究它们的另一个功能吗?

用户类别:

  public database_ab getData(Query query)
    {

        var data = db.database_ab.AsQueryable();

        if (query.name != null)
        {
            data = data.Where(c => c.Name == query.name);
        }

        if (query.price != null)
        {
            data = data.Where(c => c.Price == query.price);
        }

        return data.Any();
    }

项目控制器:

public HttpResponseMessage getData([FromUri] Query query)
    {
        User layer = new User();
       // if (User.IsInRole("user"))
       // {
            var result = layer.getData(query);
            if (result == null)
            {
                // return HttpResponseMessage
                var message = string.Format("No data was found");
                return Request.CreateErrorResponse(HttpStatusCode.NotFound, message);
            }
            return Request.CreateResponse(HttpStatusCode.OK, result);
       // }
    }

非常感谢您的时间和帮助。

【问题讨论】:

    标签: c# asp.net-mvc linq linq-to-sql asp.net-web-api


    【解决方案1】:

    .Any() 的调用返回一个布尔值,指示是否找到任何记录。

    如果你想返回实际记录,试试这个:

    • return data.Any(); 更改为return data;
    • 将方法的返回类型从 database_ab 更改为 IEnumerable<database_ab>

    【讨论】:

    • 非常感谢您的回复和帮助。我之前尝试过使用 IEnumerable 方法类型,但是我无法搜索两个值,例如 api/item?name=key&price=23 并且它输出一个空白页。我需要修改项目控制器中的某些内容吗?非常感谢您的帮助
    • 嗨,我也用 List 试过了,但我仍然遇到同样的问题。
    【解决方案2】:

    您可以简单地返回数据变量对象,即

    public IEnumerable<database_ab> getData(Query query)
        {
    
            var data = db.database_ab.AsQueryable();
    
            if (query.name != null)
            {
                data = data.Where(c => c.Name == query.name);
            }
    
            if (query.price != null)
            {
                data = data.Where(c => c.Price == query.price);
            }
    
            return data;
        }
    

    【讨论】:

    • 非常感谢您的回复和帮助。我之前尝试过使用 IEnumerable 方法类型,但是我无法搜索两个值,例如 api/item?name=key&price=23 并且它输出一个空白页。我需要修改项目控制器中的某些内容吗?非常感谢您的帮助
    【解决方案3】:

    Any 扩展方法的返回类型为 bool 类型;如果集合中的某些项目与某个给定标准匹配,则它旨在返回 true。语法上正确的是只返回data 而不是data.Any();也许这已经解决了问题。

    【讨论】:

      【解决方案4】:

      你应该做两处改变:首先改变返回类型

      //you are returning a single element
      public database_ab getData(Query query)
      

      //you are going to return many elements
      public IEnumerable<database_ab> getData(Query query)
      

      因此只需返回您的 data 对象

      return data;
      }
      

      方法.Any() 表示“集合中的任何项是否满足此条件?”,如

      var Coll = new List<int> { 1, 2, 3, 4 };
      // is any element of Coll greater than 2 ?
      var myCondition = Coll.Any(element => element > 2);
      

      【讨论】:

        【解决方案5】:

        return data.Any(); 没有任何意义。

        Any() 根据您的条件返回布尔值。

        eg: data = data.Any(c => c.Name == query.name); 
        

        根据条件结果返回布尔值。

        如果你想返回所有的数据就这样做

        return data;
        

        return data.Tolist();  // if you want the result in list.
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-11-25
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-06-02
          • 2012-12-02
          相关资源
          最近更新 更多