I am fairly new to ASP.NET Web API.
I am trying to build an API method that can handle multiple values for a single parameter from a search client.
The criteria needs to be a OR search for eg. A color search for Red OR Blue OR Green.
I pass the colors as integer to my method. Please find my controller method below which fails to compile with the following error
Errors:
Error 1 'int' does not contain a definition for 'Contains' and the best extension method overload 'System.Linq.ParallelEnumerable.Contains(System.Linq.ParallelQuery, TSource)' has some invalid arguments
Error 2 Instance argument: cannot convert from 'int' to 'System.Linq.ParallelQuery'
Code:
public IQueryable<Items> GetItemByColour(string Colour)
{
int[] intArr = Array.ConvertAll(Colour.Split(','), s => int.Parse(s));
var query = from a in db.Items
where a.ItemsColour.Contains(intArr)
select a;
return query;
}
As mentioned, Web API controller can include multiple Get methods with different parameters and types. Let's add following action methods in StudentController to demonstrate how Web API handles multiple HTTP GET requests.
You seem to have two different issues there. The first one is that you don't need to send the Colors list as a string and convert it to an integer array manually, you can do it like this instead:
public IQueryable<Items> GetItemByColour([FromUri] int[] Colour)
And you're getting an error in your LINQ query because a.ItemColour
is an integer, not an integer list, therefore you can't use the Contains
extension method on it. You have to do it like this (assuming Colour
is now an integer array):
var query = from a in db.Items
where Colour.Contains(a.ItemsColour)
select a;
return query;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With