Sql server 使用DbGeography距离和实体框架时响应为空
我知道我的数据库包含一篇具有以下两个属性的文章:Sql server 使用DbGeography距离和实体框架时响应为空,sql-server,linq,entity-framework,linq-to-entities,latitude-longitude,Sql Server,Linq,Entity Framework,Linq To Entities,Latitude Longitude,我知道我的数据库包含一篇具有以下两个属性的文章: latitude: 44.611589 longitude: -122.215347 我正在写一个方法来查找数据库中距离该位置50.0米以内的所有帖子。(因此,在这种情况下,因为有一个具有这些精确坐标的柱,它的距离为0米,应该包括在内) 这是我的密码: DbGeography queryLocation = DbGeography.FromText("POINT(-122.215437 44.611589)", 4326); double
latitude: 44.611589
longitude: -122.215347
我正在写一个方法来查找数据库中距离该位置50.0米以内的所有帖子。(因此,在这种情况下,因为有一个具有这些精确坐标的柱,它的距离为0米,应该包括在内)
这是我的密码:
DbGeography queryLocation = DbGeography.FromText("POINT(-122.215437 44.611589)", 4326);
double d = (double) DbGeography.FromText(
"POINT(-122.215347 44.611589)"
, 4326)
.Distance(queryLocation);
//d is equal to 0 when I execute this, as expected.
IQueryable<Post> posts = db.Posts.Where(p => (double) DbGeography.FromText(
"POINT(" + SqlFunctions.StringConvert(p.Longitude) + " " + SqlFunctions.StringConvert(p.Latitude) + ")"
, 4326).Distance(queryLocation) <= 50.0);
//posts is an empty collection when this executes.
这将正确返回具有正确lat/long值的post。因此,我认为假定SqlFunctions.StringConvert()
不起作用是公平的,但我没有办法测试它,因为它似乎在LINQ表达式之外不起作用。此外,String.format和double.toString()在LINQ表达式中不起作用
确认,这是SqlFunctions.StringConvert()的问题
因此,从技术上讲,我可以编写自己的SQL。耶。现在有没有办法通过LINQ到实体来实现这一点?请尝试PointFromText而不是FromText:
DbGeography queryLocation = DbGeography.PointFromText("POINT(-122.215437 44.611589)", 4326);
double d = (double) DbGeography.PointFromText(
"POINT(-122.215347 44.611589)"
, 4326)
.Distance(queryLocation);
//d is equal to 0 when I execute this, as expected.
IQueryable<Post> posts = db.Posts.Where(p => (double) DbGeography.PointFromText(
"POINT(" + SqlFunctions.StringConvert(p.Longitude) + " " + SqlFunctions.StringConvert(p.Latitude) + ")"
, 4326).Distance(queryLocation) <= 50.0);
DbGeography queryLocation=DbGeography.PointFromText(“POINT(-122.215437 44.611589)”,4326;
double d=(double)DbGeography.PointFromText(
“点(-122.215347 44.611589)”
, 4326)
.距离(查询位置);
//当我执行此操作时,d等于0,如预期的那样。
IQueryable posts=db.posts.Where(p=>(double)DbGeography.PointFromText(
“点(“+SqlFunctions.StringConvert(p.Latitude)+”“+SqlFunctions.StringConvert(p.Latitude)+”“”
距离(查询位置)
var posts = db.Posts
.Where(p => (double) DbGeography.FromText(
"POINT(" + SqlFunctions.StringConvert(-122.215347) + " " + SqlFunctions.StringConvert(47.611589) + ")"
, Constants.MetersCoordinateSystemId)
.Distance(queryLocation) <= 50.0);
SELECT
[Extent1].[Id] AS [Id],
[Extent1].[UserId] AS [UserId],
[Extent1].[Latitude] AS [Latitude],
[Extent1].[Longitude] AS [Longitude]
FROM [dbo].[Posts] AS [Extent1]
WHERE (geography::STGeomFromText(N'POINT(' + STR(cast(-122.215347 as float(53))) + N' ' + STR(cast(44.611589 as float(53))) + N')', 4326).STDistance(@p__linq__0)) <= cast(50 as float(53))
SELECT
[Extent1].[Id] AS [Id],
[Extent1].[UserId] AS [UserId],
[Extent1].[Latitude] AS [Latitude],
[Extent1].[Longitude] AS [Longitude]
FROM [dbo].[Posts] AS [Extent1]
WHERE (geography::STGeomFromText(N'POINT(' + LTRIM(Str(-122.215347, 25, 5))
+ N' ' + LTRIM(Str(44.611589, 25, 5)) + N')', 4326).STDistance(geography::STGeomFromText(N'POINT(-122.215347 47.611589)',4326)) <= 50)
DbGeography queryLocation = DbGeography.PointFromText("POINT(-122.215437 44.611589)", 4326);
double d = (double) DbGeography.PointFromText(
"POINT(-122.215347 44.611589)"
, 4326)
.Distance(queryLocation);
//d is equal to 0 when I execute this, as expected.
IQueryable<Post> posts = db.Posts.Where(p => (double) DbGeography.PointFromText(
"POINT(" + SqlFunctions.StringConvert(p.Longitude) + " " + SqlFunctions.StringConvert(p.Latitude) + ")"
, 4326).Distance(queryLocation) <= 50.0);