Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wrong distance values with GroupBy #16648

Closed
vfichot opened this issue Jul 18, 2019 · 2 comments
Closed

Wrong distance values with GroupBy #16648

vfichot opened this issue Jul 18, 2019 · 2 comments

Comments

@vfichot
Copy link

vfichot commented Jul 18, 2019

The following query with GroupBy gives wrong results for distance (Geography)

 var point = Utilities.CreatePoint(-1.209424, 44.599878);

BugDistance.zip

var query = from table1 in context.Table1
                   select new
                   {
                          Code = table1.Code,
                           Distance = table1.Location.Distance(point)
                   };

var value = query.First();

// OK : 543758 m
Console.WriteLine($"Code {value.Code}, Distance: {value.Distance}");

var value2 = query.GroupBy(p => p.Code).Select(grp => grp.First()).First();

// KO ; 5.51 ?????

BugDistance.zip

 Console.WriteLine($"Code {value2.Code}, Distance: {value2.Distance}");

Further technical details

EF Core version: 2.2.6
Database Provider: Microsoft.EntityFrameworkCore.SqlServer
Database: SQL Server Express 2014 SP3
Operating system: 8.1
IDE: Visual Studio 2017 15.9.14

@ajcvickers
Copy link
Member

Note for triage: client-eval of the GrioupBy query.

dbug: Microsoft.EntityFrameworkCore.Query[10101]
      => Microsoft.EntityFrameworkCore.Query.RelationalQueryModelVisitor
      Compiling query model:
      '(from IGrouping<string, <>f__AnonymousType1<string, double>> grp in
          (from Table1 table1 in DbSet<Table1>
          select new {
              [table1].Code,
              double [table1].Location.Distance(__point_0)
           }).GroupBy([table1].Code, new {
                  [table1].Code,
                  double [table1].Location.Distance(__point_0)
               })
      select
          (from <>f__AnonymousType1<string, double> <generated>_1 in [grp]
          select [<generated>_1]).First()).First()'
warn: Microsoft.EntityFrameworkCore.Query[10103]
      => Microsoft.EntityFrameworkCore.Query.RelationalQueryModelVisitor
      Query: '(from <>f__AnonymousType1<string, double> <generated>_1 in [grp] select [<generated>_1]).First()' uses First/FirstOrDefault/Last/LastOrDefault operation without OrderBy and filter which may lead to unpredictable results.
warn: Microsoft.EntityFrameworkCore.Query[10103]
      => Microsoft.EntityFrameworkCore.Query.RelationalQueryModelVisitor
      Query: '(from IGrouping<string, <>f__AnonymousType1<string, double>> grp in  (from Table1 table1 in DbSet<Ta...' uses First/FirstOrDefault/Last/LastOrDefault operation without OrderBy and filter which may lead to unpredictable results.
dbug: Microsoft.EntityFrameworkCore.Query[10104]
      => Microsoft.EntityFrameworkCore.Query.RelationalQueryModelVisitor
      Optimized query model:
      '(from IGrouping<string, <>f__AnonymousType1<string, double>> grp in
          (from Table1 table1 in DbSet<Table1>
          select new {
              [table1].Code,
              double [table1].Location.Distance(__point_0)
           }).GroupBy([table1].Code, new {
                  [table1].Code,
                  double [table1].Location.Distance(__point_0)
               })
      select
          (from <>f__AnonymousType1<string, double> <generated>_1 in [grp]
          select [<generated>_1]).First()).First()'
warn: Microsoft.EntityFrameworkCore.Query[20500]
      => Microsoft.EntityFrameworkCore.Query.RelationalQueryModelVisitor
      The LINQ expression 'GroupBy([table1].Code, new <>f__AnonymousType1`2(Code = [table1].Code, Distance = [table1].Location.Distance(__point_0)))' could not be translated and will be evaluated locally.
warn: Microsoft.EntityFrameworkCore.Query[20500]
      => Microsoft.EntityFrameworkCore.Query.RelationalQueryModelVisitor
      The LINQ expression 'First()' could not be translated and will be evaluated locally.
warn: Microsoft.EntityFrameworkCore.Query[20500]
      => Microsoft.EntityFrameworkCore.Query.RelationalQueryModelVisitor
      The LINQ expression 'First()' could not be translated and will be evaluated locally.
warn: Microsoft.EntityFrameworkCore.Query[20500]
      => Microsoft.EntityFrameworkCore.Query.RelationalQueryModelVisitor
      The LINQ expression 'First()' could not be translated and will be evaluated locally.
dbug: Microsoft.EntityFrameworkCore.Query[10107]
      => Microsoft.EntityFrameworkCore.Query.RelationalQueryModelVisitor
      (QueryContext queryContext) => IEnumerable<<>f__AnonymousType1<string, double>> _InterceptExceptions(
      |__ source: IEnumerable<<>f__AnonymousType1<string, double>> _ToSequence(() => <>f__AnonymousType1<string, double> First(IEnumerable<<>f__AnonymousType1<string, double>> _Select(
      |   |__ source: IEnumerable<IGrouping<string, <>f__AnonymousType1<string, double>>> _GroupBy(
      |   |   |__ source: IEnumerable<ValueBuffer> _ShapedQuery(
      |   |   |   |__ queryContext: queryContext,
      |   |   |   |__ shaperCommandContext: SelectExpression:
      |   |   |   |       SELECT [table1].[Code], [table1].[Location].STDistance(@__point_0) AS [Distance], [table1].[Location]
      |   |   |   |       FROM [Table1] AS [table1]
      |   |   |   |       ORDER BY [table1].[Code],
      |   |   |   |__ shaper: ValueBufferShaper),
      |   |   |__ keySelector: (ValueBuffer table1) => string TryReadValue(table1, 0, Table1.Code),
      |   |   |__ elementSelector: (ValueBuffer table1) => new {
      |   |           string TryReadValue(table1, 0, Table1.Code),
      |   |           double IGeometry TryReadValue(table1, 2, Table1.Location).Distance(IPoint GetParameterValue(
      |   |           |__ queryContext: queryContext,
      |   |           |__ parameterName: "__point_0"))
      |   |        }),
      |   |__ selector: (IGrouping<string, <>f__AnonymousType1<string, double>> grp) => <>f__AnonymousType1<string, double> First(grp)))),
      |__ contextType: BugDistance.Models.TestStoreContext,
      |__ logger: DiagnosticsLogger<Query>,
      |__ queryContext: queryContext)
dbug: Microsoft.EntityFrameworkCore.Database.Connection[20000]
      Opening connection to database 'TestStore' on server '.\SQlEXPRESS'.
dbug: Microsoft.EntityFrameworkCore.Database.Connection[20001]
      Opened connection to database 'TestStore' on server '.\SQlEXPRESS'.
dbug: Microsoft.EntityFrameworkCore.Database.Command[20100]
      Executing DbCommand [Parameters=[@__point_0='?' (Size = 22) (DbType = Binary)], CommandType='Text', CommandTimeout='30']
      SELECT [table1].[Code], [table1].[Location].STDistance(@__point_0) AS [Distance], [table1].[Location]
      FROM [Table1] AS [table1]
      ORDER BY [table1].[Code]

@smitpatel
Copy link
Member

Duplicate of #12088

@smitpatel smitpatel marked this as a duplicate of #12088 Jul 18, 2019
@ajcvickers ajcvickers reopened this Oct 16, 2022
@ajcvickers ajcvickers closed this as not planned Won't fix, can't repro, duplicate, stale Oct 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants