如何在AsQueryable方法中使用'Any'?

use*_*072 2 c# linq asp.net-mvc linq-to-sql asp.net-web-api

我试图让我的查询输出所有数据,如果给出任何参数'name'和'price'或仅名称.我尝试使用'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();
    }
Run Code Online (Sandbox Code Playgroud)

物品控制器:

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);
       // }
    }
Run Code Online (Sandbox Code Playgroud)

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

Gra*_*ICA 5

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

如果要返回实际记录,请尝试以下操作:

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