Linq无法识别方法GetDistanceTo [英] Linq does not recognize method GetDistanceTo
问题描述
我有这个查询,我试图按距离排序.但是,linq向我抛出一个错误,说它无法识别方法GetDistanceTo.该查询在OrderBy子句被取出时起作用.
I have this query which im attempting to sort by distance. However, linq is throwing me an error saying it does not recognize the method GetDistanceTo. The query works when the OrderBy clause is taken out.
var coord = new GeoCoordinate { Latitude = (double?)array.latitude ?? 0, Longitude = (double?)array.longitude ?? 0 };
var property = db.Properties.Select(x => new SearchResultsViewModel
{
geocoord = new GeoCoordinate { Latitude = (double?)x.latitude ?? 0, Longitude = (double?)x.longitude ?? 0 }
}).OrderBy(x=>x.geocoord.GetDistanceTo(coord)).ToList();
推荐答案
LINQ to Entities必须将您的表达式转换为可以对数据库执行的SQL查询.它不知道如何将GetDistanceTo
转换为SQL查询.
LINQ to Entities has to translate your expression to a SQL query that can be executed against a database. It does not know how to translate GetDistanceTo
into a SQL query.
您可以在OrderBy
之前调用AsEnumerable
,以强制将排序作为内存中的LINQ to Objects查询进行.
You can call AsEnumerable
before OrderBy
to force ordering to be performed as in-memory LINQ to Objects query.
var property = db.Properties.Select(x => new SearchResultsViewModel
{
geocoord = new GeoCoordinate { Latitude = (double?)x.latitude ?? 0, Longitude = (double?)x.longitude ?? 0 }
}).AsEnumerable().OrderBy(x=>x.geocoord.GetDistanceTo(coord)).ToList();
这篇关于Linq无法识别方法GetDistanceTo的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!