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

ore*_*pan 7 c# linq asp.net function

我试过了:

 var doctorPractise = from d in db.DoctorsPrivateClinics
                                     where  GetDistance(db, d)<1.0
                                     select d;
Run Code Online (Sandbox Code Playgroud)

但这不起作用,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;
    }
Run Code Online (Sandbox Code Playgroud)

我想要实现的是计算地图上某个特定点(cit.Name)与私人诊所的位置之间的距离.已从Google地图中检索地图上特定点的经度和纬度.在我的查询中,我指定此距离必须小于1公里

spe*_*der 11

问题是您的查询是由LINQ提供程序(EF/Linq2Sql?)转换为SQL的.它无法翻译任意.net方法.解决这个问题的唯一方法是在本地枚举整个集合,无需使用索引等数据库,还可能从数据库中获取网络IO的瓶颈.

如果这不是问题:

var doctorPractise = from d in db.DoctorsPrivateClinics.AsEnumerable()
                                 where  GetDistance(db, d)<1.0
                                 select d;
Run Code Online (Sandbox Code Playgroud)

否则,请考虑以可转换为SQL的术语重新表达您的查询.看到你的GetDistance方法将有助于我们在这里帮助你.