score:7

Accepted answer

This should work in Linq to Objects if GetDistance() returns a boolean - it will not work with Linq to Entities since it will try to map your method to a SQL equivalent, which of course there is none.

As a crude workaround you could use AsEnumerable() but that would materialize all your types so is not recommended if your table is larger:

myTypes = myTypes.AsEnumerable()
                 .Where(x => GetDistance(x.Zip, givenZip) < 10);

Another way would be to map Zip codes to geographic locations in the database and use those locations directly with the soon to be supported spatial data types - this is probably the best approach in my opinion, but not production-ready. Of course if you are restricted to just SQL Server you could just use a store query directly to use geo-locations - but that would work around EF.

score:0

Assuming:

List<myType> myTypes;

try:

myTypes = myTypes.Where(x => GetDistance(x.Zip, givenZip) < 10).ToList();

score:3

This will throw an error because the runtime tries to convert your expression tree into SQL. The function 'GetDistance' cannot be converted.

Have a look at Model Defined Functions. They allow you to define a custom function in your edmx which you can execute when building queries.


Related Query

More Query from same tag