RPM1984
RPM1984

Reputation: 73123

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:

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?

Upvotes: 5

Views: 3354

Answers (2)

Ray Cheng
Ray Cheng

Reputation: 12586

You need to construct a list of the location ID can be searched.

var idsToSearch = "1,2,3,4,5...";    

Then you can use Contains in the following way:

var x = db
       .Users
       .Where(u => idsToSearch.Contains(u.LocationId));

Upvotes: 0

aqwert
aqwert

Reputation: 10789

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.

Upvotes: 1

Related Questions