c# – 如何在LINQ查询中调用本地函数?
发布时间:2020-12-15 17:45:57 所属栏目:百科 来源:网络整理
导读:我试过了: var doctorPractise = from d in db.DoctorsPrivateClinics where GetDistance(db,d)1.0 select d; 但这不起作用,GetDistance是一个本地函数,我没有得到一个例外但它似乎没有工作,任何想法? 谢谢 GetDistance是: private double GetDistance(DB
我试过了:
var doctorPractise = from d in db.DoctorsPrivateClinics where GetDistance(db,d)<1.0 select d; 但这不起作用,GetDistance是一个本地函数,我没有得到一个例外但它似乎没有工作,任何想法? 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; } 我想要实现的是计算地图上某个特定点(cit.Name)与私人诊所的位置之间的距离.已从Google地图中检索地图上特定点的经度和纬度.在我的查询中,我指定此距离必须小于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方法可以帮助我们在这里帮助你. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |