I'm running into an interesting Linq to Sitecore issue. I have the case where I have a directory of people and a directory of locations that are all stored in Sitecore. I'm trying to write a search function that will allow me to search for people nearby. Adding a wrinkle to this is the fact that people can be associated with multiple locations.
So far the approach has been to find nearby locations, get their id's then find all of the people who are associated with those locations.
This looks roughly like:
var locations = GetNearbyLocations(lat, long, radius) // returns a list of short ID's
var searchPredicate = PredicateBuilder.True<SearchResultItem>();
var locationsPredicate = PredicateBuilder.False<SearchResultItem>();
foreach (var location in locations)
{
locationPredicate = locationPredicate.Or(x => x["location"] == location);
}
searchPredicate = searchPredicate.And(locationPredicate);
searchPredicate = searchPredicate.And(sri => sri.TemplateId == personTemplateId);
using (var context = index.CreateSearchContext())
{
var peopleReults = context.GetQueryable<SearchResultItem>.Where(locationPredicate).GetResults();
}
The above works great if GetNearbyLocations
returns a relatively small set of locations. Once we get over 150 or so, the call to GetQuerable
will result in a stack overflow.
You can probably use the locations array and do a contains.
If you get a big fat error from that Ill post some lambda expressions that you can use instead.