gpt4 book ai didi

c# - 如何在 LINQ 查询中调用本地函数?

转载 作者:太空狗 更新时间:2023-10-29 20:00:01 28 4
gpt4 key购买 nike

我试过:

 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;
}

我想要实现的是计算 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/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com