这是一个SQL语句,返回半径30英里范围内的用户列表
SELECT
id, (
3959 * acos (
cos ( radians(78.3232) )
* cos( radians( lat ) )
* cos( radians( lng ) - radians(65.3234) )
+ sin ( radians(78.3232) )
* sin( radians( lat ) )
)
) AS distance
FROM users
HAVING distance < 30
ORDER BY distance
我正在使用c# .NET Web API2,是否有可能将上述转换为LINQ语句,以便我们可以使用API获取此数据?
首先你需要为弧度定义这个方法
public static double Radians(double angle)
{
return (Math.PI / 180) * angle;
}
现在,您可以使用这个查询
var distances = users
.Where(x => x.YourFilterOnPagination)
.ToList() // your collection most be List
.Select(x => new
{
id = x.id,
distance = (395 * Math.Acos(Math.Cos(Radians(78.3232)) *
Math.Cos(Radians(x.lat)) * Math.Cos(Radians(x.lat) - Radians(65.3234)) +
Math.Sin(Radians(78.3232)) *
Math.Sin(Radians(x.lat))))
})
.Where(d => d.distance < 30)
.OrderBy(d => d.distance);
YourFilterOnPagination:如果你的数据很大,你可以限制它在w