Anthony Bobenrieth
Anthony Bobenrieth

Reputation: 2893

Compare EntityCollection<Class> in a LinqToEntity query

Is it possible to compare two EntityCollection in a Linq query?

I tried this way:

from t in _bdd.Table
where (idList).All(id=>  t.ids.Contains(id))
                         select i).FirstOrDefault()

where idList and ids are both EntityCollection

but i got a NotSupportedException:

"Unable to create a constant value of type (ID) Only primitive types ('such as Int32, String, and Guid') are supported in this context"

Does there is no way to compare two List in a single Linq query?

Upvotes: 0

Views: 540

Answers (1)

Mohammad
Mohammad

Reputation: 1990

First the exception is caused by using a local collection (idList) as the source in the where clause. Instead you should try to switch sides it so it appears on the right hand side may be inside some LINQ method.

I think that this where query will get you the result you want:

where t.ids.Count() == idList.Count() && t.ids.All(ids => idList.Contains(id))

Also the "select i" part is not clear what it selects. There no variable in your code snippet named i.

Upvotes: 0

Related Questions