Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error with Entity Framework .Any() filter

Here's my query:

var x = db
   .Users
   .Where(u => u.Locations.Any(l => searchedLocation.Counties.Any(c => c.LocationId == l.LocationId));

Context:

  • Users is IQueryable<User>. (EF object set)
  • searchedLocation is a Location object.
  • Counties is a ICollection<MiniLocation>.

What i'm trying to do:

Return all users, where any of the Counties for those locations have a locationId of any of the counties belonging to the searched location.

Example:

Search for New York City (LocationId = 1. County1LocationId = 2)

User: Bob. Locations: Soho. County1LocationId = 2. County2 LocationId = 3.

So that's a match. (because Soho have a County with a LocationId of 2, and so does NYC)

Error i receive:

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

Any ideas?

like image 315
RPM1984 Avatar asked Nov 14 '22 06:11

RPM1984


1 Answers

This MSDN page states that this construct is not supported. You can use this method for .Net 3.5 Linq to Entities to help replace the use of Any.

like image 137
aqwert Avatar answered Dec 10 '22 13:12

aqwert