6

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

2 Answers 2

4

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;
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for the quick response, its definitely the correct way. The only problem is I am using WP8 app to consume the Web API and use a Web Client. Not sure how you would pass an array using a Web Client
It's fairly easy, you do it like this: example-url.com/?Colour=1&Colorur=2&Colour=3. In this example, every instance of the Colour query-string variable will be an item in the Colour array. This way your service is also more interoperable. Regarding your question, I did also point out the other issue you had and how to solve it. :)
This is by far the best way to go. By passing a string, it requires you to do type checking to prevent any int.Parse FormatExceptions.
that's perfect! Wasn't aware of that. Wasted 3 hours of my life trying to get it work earlier!
0

I suppose this will be the solution to your issue:

public IQueryable<Items> GetItemByColour(string Colour)
{
    List<int> intArr = Array.ConvertAll(Colour.Split(','), s => int.Parse(s)).ToList();

    var query = from a in db.Items
                where a.intArr.Contains(ItemsColour)
                select a;

    return query;
}

The problem was that you were looking for the presence of an array in an int. You need to look for the presence of an int in the array

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.