我试过了:
var doctorPractise = from d in db.DoctorsPrivateClinics
where GetDistance(db, d)<1.0
select d;
但这不起作用,GetDistance 是一个本地函数,我没有得到异常(exception)但它似乎不起作用,有什么想法吗?
谢谢
GetDistance 是:
private double GetDistance(DBClassesDataContext db, DoctorsPrivateClinic docPra)
{
double distance=-1;
double longitude;
double latitude;
var city = from c in db.Cities
where c.Code == Convert.ToInt32(Request.QueryString["Area"])
select c;
foreach (var cit in city)//it will be just one row that will be found but I don't know how to express it without foreach
{
Calculations.GetLongitudeLatitudeGoogle getLongLat = new Calculations.GetLongitudeLatitudeGoogle();
getLongLat.GetLongitudeLatitude("", cit.Name, "", out longitude, out latitude);
distance = CalcualateDistance(Convert.ToDouble(docPra.PrivateClinic.Latitude), Convert.ToDouble(docPra.PrivateClinic.Longtitude), latitude, longitude);
}
return distance;
}
我想要实现的是计算 map 上特定点(cit.Name)与私有(private)诊所位置之间的距离。 map 上特定点的经度和纬度是从 Googlemaps 中检索到的。在我的查询中,我指定此距离必须小于 1 公里
最佳答案
问题是您的查询正在被 LINQ 提供程序(EF/Linq2Sql?)转换为 SQL。它无法翻译任意 .net 方法。解决这个问题的唯一方法是在本地枚举整个集合,省去索引等的数据库优势,并且可能会阻碍来自数据库的网络 IO。
如果这不是问题:
var doctorPractise = from d in db.DoctorsPrivateClinics.AsEnumerable()
where GetDistance(db, d)<1.0
select d;
否则,请考虑使用可转换为 SQL 的术语重新表述您的查询。看到您的
GetDistance
方法将有助于我们在这里为您提供帮助。关于c# - 如何在 LINQ 查询中调用本地函数?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/13105518/