【问题标题】:How to call user defined function in Lambda .NET Syntax如何在 Lambda .NET 语法中调用用户定义的函数
【发布时间】:2012-05-10 12:37:21
【问题描述】:

好的,这是我的问题。我有数据库表BloodCenters,其中有latitudelongitude 字段。我想使用 Linq Lambda 语法通过检索相对于某个点在给定半径内的 BloodCenters 来查询这些表。

我正在使用这段代码,但它仍然给我错误(在 Where 谓词内):

public static List<BloodCenter> GetAllNearby(int latitude, int longitude, int radius, int limit){
    List<BloodCenter> res = null;
    res = new DBEntities().BloodCenters.Where(b =>
        Util.distance(latitude, longitude, b.Latitude, b.Longitude, "K") <= radius).Take(limit);
    return res;
}

Util.distance 是一个返回纬度和经度 2 点之间距离的函数。 已编辑 所以,我不能把自己的函数放在 Where 谓词中。但是我可以使用 SQL 语法计算距离吗?这是我的Util.Distance 实现:

public static double distance(double lat1, double longi1, double lat2, double longi2, string unit){
    var theta = longi1 - longi2;
    var dist = Math.Sin(DegreeToRadian(lat1)) * Math.Sin(DegreeToRadian(lat2)) +
        Math.Cos(DegreeToRadian(lat1)) * Math.Cos(DegreeToRadian(lat2)) * Math.Cos(DegreeToRadian(theta));
    dist = Math.Acos(dist);
    dist = RadianToDegree(dist);
    var miles = dist * 60 * 1.1515;
    unit = unit.ToUpper();
    if (unit.Equals("K"))
        return miles * 1.609344;
    else if (unit.Equals("N"))
        return miles * 0.8684;
    else
        return miles;
}

【问题讨论】:

  • 经纬度为int's?
  • 你应该尝试使用 sql server 空间数据类型而不是一些托管代码函数

标签: c# .net linq linq-to-sql lambda


【解决方案1】:

这行不通,因为Util.distance 是在您的 C# 代码中实现的,并且查询界面不知道如何将其转换为 SQL(或者即使可能)。

一个直接的解决方法是从数据库中获取所有记录并将谓词应用于实例,如下所示:

res = new DBEntities().BloodCenters.ToList()
          .Where(b => Util.distance(latitude, longitude, b.Latitude, b.Longitude, "K") <= radius)
          .Take(limit);
return res;

当然,获取所有记录也不是最好的主意,但有时根本没有其他办法。

您可以尝试将Util.distance 的逻辑内联到Where 谓词中。如果逻辑可以仅使用“简单”操作(如基本数学)来表达,则查询界面将能够将其转换为 SQL,一切都会顺利进行。

Here 是可以自动转换为 SQL 的所有方法的列表。

【讨论】:

  • 感谢您的解释。不幸的是,我在您的链接上没有看到任何三角函数。我通过提供Util.Distance 实现的细节来编辑我的问题。
【解决方案2】:

您必须将DBEntities().BloodCenters 转换为IEnumerableList 或其他本地存储的内容。 Linq to SQL 将 C# 转换为 SQL - 因此您只能使用 SQL 服务器提供的函数。

另一种方法是用可翻译的语句模拟函数或在数据库中实现它(存储过程或类似的)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-11-06
    • 2023-03-29
    • 2017-07-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多