我已经实现了与 Nerddinner 中相同的函数“distancebetween”。我创建了一个机场存储库并拥有以下方法:
public IQueryable<AllAirports> ReturnAllAirportWithIn50milesOfAPoint(double lat, double lon)
{
var airports = from d in im.AllAirports
where DistanceBetween(lat, lon, (double)d.Lat, (double)d.Lon) < 1000.00
select d;
return airports;
}
[EdmFunction("AirTravelModel.Store", "DistanceBetween")]
public static double DistanceBetween(double lat1, double long1, double lat2, double long2)
{
throw new NotImplementedException("Only call through LINQ expression");
}
当我测试它时,它显示:
base {“类型'AirTravelMVC3.Models.Repository.AirportRepository'的指定方法'Double DistanceBetween(Double,Double,Double,Double)'无法转换为LINQ to Entities存储表达式,因为没有重载与传递的参数匹配。” } System.SystemException {System.NotSupportedException}
您对为什么会发生这种情况有任何想法吗?我的工作和 nerddinner 的唯一不同是我在实体框架中使用了 POCO 插件。
SQL UDF如下,它在数据库中运行良好:
创建函数 [dbo].[DistanceBetween](@Lat1 真实, @Long1 真实,@Lat2 真实,@Long2 真实) 返回真实 作为 开始 将@dLat1InRad 声明为 float(53); SET @dLat1InRad = @Lat1 * (PI()/180.0); 将@dLong1InRad 声明为 float(53); SET @dLong1InRad = @Long1 * (PI()/180.0); 将@dLat2InRad 声明为 float(53); SET @dLat2InRad = @Lat2 * (PI()/180.0); 将@dLong2InRad 声明为 float(53); SET @dLong2InRad = @Long2 * (PI()/180.0); 将@dLongitude 声明为浮点数(53); SET @dLongitude = @dLong2InRad - @dLong1InRad; 将@dLatitude 声明为浮点数(53); SET @dLatitude = @dLat2InRad - @dLat1InRad; /* 中间结果 a. */ 将@a 声明为浮点数(53); SET @a = SQUARE (SIN (@dLatitude / 2.0)) + COS (@dLat1InRad) * COS (@dLat2InRad) * SQUARE(SIN (@dLongitude / 2.0)); /* 中间结果 c(以弧度表示的大圆距离)。*/ 将@c 声明为真实的; SET @c = 2.0 * ATN2 (SQRT (@a), SQRT (1.0 - @a)); 将@kEarthRadius 声明为真实; /* SET kEarthRadius = 3956.0 英里 */ 设置@kEarthRadius = 6376.5; /* 公里 */ 将@dDistance 声明为真实; 设置@dDistance = @kEarthRadius * @c; 返回(@dDistance); 结尾