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

  • Thread starter Thread starter shapper
  • Start date Start date
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
 
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
 
Back
Top