I am trying to get my query to output all data, if given any parameter 'name' and 'price' or name only. I tried using 'Any' parameter but it gives me -- Cannot implicitly convert type 'bool' to -- error.

I was previously using 'FirstOrDefault' but that was only outputting the first record details.

Is their another function i can use or look into?

User class:

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

Items Controller:

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

Many thanks for your time and help.

有帮助吗?

解决方案

The call to .Any() returns a boolean value indicating whether or not any records were found.

If you want to return the actual records, try this:

  • Change return data.Any(); to return data;
  • Change the return type on your method from database_ab to IEnumerable<database_ab>.

其他提示

You can simply return the data variable object i.e.

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

The return type of the Any extension method is of type bool; it is intended to return true if some item of the collection matches some given criterion. Syntactically correct would be to just return data instead of data.Any(); perhaps this already solves the problem.

You should make two changes: first change the return type

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

to

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

and consequently simply return your data object

return data;
}

The method .Any() means "Does any of the items in the collection satisfy this condition?", as in

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

return data.Any(); Doesn't make any sense.

Any() returns boolean value based on your condition.

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

returns boolean values based on the result of the condition.

If you want to return all the data just do

return data;

or

return data.Tolist();  // if you want the result in list.
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top