下面是我寫的Linq to Entities Statement。LINQ to Entities無法識別該方法,並且此方法無法轉換爲商店表達式
public static List<Model.User> GetNearestUsers(int userid,int count,ref Model.HangoutDBEntities context)
{
Model.Location myLocation=GetCurrentLocation(userid,ref context);
return context.Locations.Where(o => EntityFunctions.DiffMinutes(DateTime.Now, o.DateTimeStamp) <= 30).OrderBy(o => Core.Location.Distance.CalculateDistance(myLocation.Latitude, myLocation.Longitude, o.Latitude, o.Longitude)).Select(o => o.User).ToList();
}
而這裏的CalculateDistance方法
public static double CalculateDistance(decimal lat1,decimal lon1,decimal lat2,decimal lon2)
{
try
{
var R = 6371; // Radius of the earth in km
var dLat = DegreeToRadian((double)(lat2 - lat1)); // Javascript functions in radians
var dLon = DegreeToRadian((double)(lon2 - lon1));
var a = Math.Sin(dLat/2) * Math.Sin(dLat/2) +
Math.Cos(DegreeToRadian((double)lat1)) * Math.Cos(DegreeToRadian((double)lat2)) *
Math.Sin(dLon/2) * Math.Sin(dLon/2);
var c = 2 * Math.Atan2(Math.Sqrt(a), Math.Sqrt(1 - a));
var d = R * c; // Distance in km
return (double)d;
}
catch
{
return 0.0;
}
}
public static double DegreeToRadian(double angle)
{
return Math.PI * angle/180.0;
}
請讓我知道,如果有這方面的任何變通辦法。我得到一個異常
LINQ到實體無法識別方法「雙 CalculateDistance(System.Decimal,System.Decimal,System.Decimal, System.Decimal)」的方法,而這種方法不能被翻譯成一個 存儲表達式。
我知道自定義函數不適用於Linq 2實體,但我不知道這個解決方法。你能幫我解決嗎? :)
如果您正在使用sql-server,我強烈建議您查看使用空間數據類型而不是在內存中進行計算。問題是您的計算無法轉換爲SQL.http://msdn.microsoft.com/en-us/library/bb933876(v = sql.105).aspx – StuartLC