如何将此SQL转换为VB.NET LINQ

时间:2015-06-24 23:56:06

标签: sql vb.net linq entity-framework

我在将此语句从SQL转换为LINQ时遇到了一些麻烦。

DECLARE @radians float
SELECT @radians = 57.295779513082323 
SELECT 
    t1.*, 
    DistanceInMiles = 
        3963.0*acos(
            sin(t1.Latitude/(@radians))
            * sin(t2.Latitude/(57.295779513082323))
            + cos(t1.Latitude/(@radians))
            * cos(t2.Latitude/(@radians)) 
            * cos(t2.Longitude/(@radians) - t1.Longitude/(@radians))
        )
FROM ZipCodeData t1
JOIN ZipCodeData t2 ON (
    3963.0*acos(
        sin(t1.Latitude/(@radians)) 
        * sin(t2.Latitude/(57.295779513082323)) 
        + cos(t1.Latitude/(@radians)) 
        * cos(t2.Latitude/(@radians)) 
        * cos(t2.Longitude/(@radians) 
        - t1.Longitude/(@radians))
    ) < 100
)
WHERE t2.ZipCode='10001'
ORDER BY DistanceInMiles

我在VB.NET工作

我不知道从哪里开始

Dim results = z1 in Entities.ZipCodeDatas

好的,我有一个开始,但认真

编辑:

Dim radians = 57.29577951309232
Dim results = From z1 In Entities.ZipCodeDatas
              z1.ZipCode
              Join z2 In Entities.ZipCodeDatas On z2.ZipCode Equals z1.ZipCode        

首先,我不知道如何创建DistanceInMiles并且我不知道如何创建第二个加入,而且我不知道如何加入小于<条款

编辑2:

Dim result = (
    From z1 In Entities.ZipCodeDatas
    Join z2 In Entities.ZipCodeDatas On z2.ZipCode Equals z1.ZipCode
    Where (
        3963.0 * Math.Acos(
            Math.Sin(z1.Latitude.Value / (radians)) _
            * Math.Sin(z2.Latitude.Value / (57.295779513082323)) _
            + Math.Cos(z1.Latitude.Value / (radians)) _
            * Math.Cos(z2.Latitude.Value / (radians)) _
            * Math.Cos(z2.Longitude.Value / (radians) - z1.Longitude.Value / (radians)) _
        )
    ) < radius
)

1 个答案:

答案 0 :(得分:1)

这样的事情:

Dim radians = 57.295779513082323
Dim result = ( _
    From t1 in Entities.ZipCodeDatas
    From t2 in Entities.ZipCodeDatas
    Where t2.ZipCode = "10001" _
    And 3963.0 * SqlFunctions.Acos( _
        SqlFunctions.Sin(t1.Latitude / (radians)) _
        * SqlFunctions.Sin(t2.Latitude/(57.295779513082323)) _
        + SqlFunctions.Cos(t1.Latitude/(radians)) _
        * SqlFunctions.Cos(t2.Latitude/(radians)) _
        * SqlFunctions.Cos(t2.Longitude/(radians) - t1.Longitude/(radians)) _
    ) < 100
    Select New With { _
        ZipCodeDatas = t1, _
        DistanceInMiles = 3963.0 * SqlFunctions.Acos( _
            SqlFunctions.Sin(t1.Latitude/(@radians)) _
            * SqlFunctions.Sin(t2.Latitude/(57.295779513082323)) _
            + SqlFunctions.Cos(t1.Latitude/(@radians)) _
            * SqlFunctions.Cos(t2.Latitude/(@radians)) _
            * SqlFunctions.Cos(t2.Longitude/(@radians) - t1.Longitude/(@radians)) _
        )
    }
).OrderBy(o => o.DistanceInMiles)