Linq不能识别GetDistanceTo方法
本文关键字:方法 GetDistanceTo 识别 不能 Linq | 更新日期: 2023-09-27 18:09:12
我有这个查询,我试图按距离排序。然而,linq向我抛出一个错误,说它不识别GetDistanceTo方法。当取出OrderBy子句时,查询工作。
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查询。
你可以在OrderBy
之前调用AsEnumerable
来强制排序执行内存LINQ to Objects查询。
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();