Method 'Boolean Contains(Int32)' has no supported translation to SQL.

S

shapper

Hello,

On a Linq query I have the following:

..Where(o => ids.Contains(o.Id) && o.SlideLocalized.Language.Code ==
language)

ids is a List<Int32>. I get the following error:

Method 'Boolean Contains(Int32)' has no supported translation to SQL.

If I change ids to and array of ints, ids[], I don't get the error.
Does this mean I can only do this using an array of ints?

Or is something wrong on how I am doing this in my Linq Query?
It is just feels strange that it works in an array but not with list.

Thanks,
Miguel
 
P

Peter Duniho

shapper said:
Hello,

On a Linq query I have the following:

..Where(o => ids.Contains(o.Id) && o.SlideLocalized.Language.Code ==
language)

ids is a List<Int32>. I get the following error:

Method 'Boolean Contains(Int32)' has no supported translation to SQL.

If I change ids to and array of ints, ids[], I don't get the error.
Does this mean I can only do this using an array of ints?

Or is something wrong on how I am doing this in my Linq Query?
It is just feels strange that it works in an array but not with list.

It is that way because List<T> has a Contains() method, while Array does
not. So, when you use an int[], the Enumerable.Contains() method is
used instead, which is supported, while List<T>.Contains() is not.

Force your List<Int32> to be IEnumerable<Int32> and I think it should work:

.Where(o => ((IEnumerable<Int32>)ids).Contains(o.Id)
&& o.SlideLocalized.Language.Code == language)

Alternatively, call the Enumerable method directly:

.Where(o => Enumerable.Contains(ids, o.Id)
&& o.SlideLocalized.Language.Code == language)

Pete
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top