Click here to Skip to main content
15,892,809 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello,

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' but it gives me -- Cannot implicitly convert type 'bool' to -- error. Is their another function i can use or do i need to look into?

Thanks in advance.

C#
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();
  }
Posted
Comments
CHill60 17-Mar-14 8:28am    
data is a var, but your function is expecting a database_ab ... whatever that is. Work out what you are actually trying to return and either derive that from within this function or change the return type of this function

You should change your method signature to:

C#
public List<database_ab> getData(Query query)</database_ab>

Then change your return statement to
C#
return data.ToList();

Your issue is that Any() returns a bool to see if there are any entries in your collection that match your query.

By making those changes you should get a list that contains 0 or more entries based on the success and failures of your other queries.
 
Share this answer
 
Comments
miss786 17-Mar-14 8:57am    
Hi, thank you for your help. I have tried using 'List' and 'IEnumerable' as a method type but this makes my querystring such as api/data?name=app&price=78, return a blank screen on the client-side. This query type only allows me to search one value, whereas I would like to search both parameters name and price. Any help please.
As per MSDN : Enumerable.Any<tsource> determines whether a sequence contains any elements.
Which means it is returning true/false (bool) value.

So either change the method signature or use other method(i.e shown in Solution #1 or may be FirstOrDefault()).

-KR
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900