Here's the Linq to Entities Statement which I've written.
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();
}
And here's the CalculateDistance Method
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;
}
Please let me know if there are any workarounds for this. I get an Exception
LINQ to Entities does not recognize the method 'Double CalculateDistance(System.Decimal, System.Decimal, System.Decimal, System.Decimal)' method, and this method cannot be translated into a store expression.
I know custom functions don't work with Linq 2 Entities but I have no idea on the workarounds for this. Can you please help me out. :)