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

Query: Wrong Sql emitted when doing a .Distinct() query on a one-to-one join #7421

Closed
shaulbehr opened this issue Jan 16, 2017 · 3 comments
Closed
Assignees
Labels
closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. type-bug
Milestone

Comments

@shaulbehr
Copy link

shaulbehr commented Jan 16, 2017

I have a Thing table, which has a one to zero/one relationship with ThingExtended:

public class Thing
{
  [Key]
  public Guid ThingId {get; set;}
  public ThingExtended ThingExtended {get; set;}
}
public class ThingExtended
{
  [Key]
  public Guid ThingId {get; set;}
  public Thing Thing {get; set;}
  public int SomeValue {get; set;}
}
...
modelBuilder.Entity<ThingExtended>( e=>
{
  e.HasOne(t => t.Thing)
    .WithOne(t => t.ThingExtended)
    .HasForeignKey<ThingExtended>(t=>t.ThingId);
});

Now I want to write a query that finds the number of distinct values for ThingExtended.SomeValue for some subset of Things:

IQueryable<Thing> thingQuery;
...
var distinctValues = thingQuery
    .Select(t => t.ThingExtended.SomeValue)
    .Distinct()
    .Count();

Ba-da-BOOM! SQL error:

System.Data.SqlClient.SqlException : The ORDER BY clause is invalid in views, inline functions, derived tables, subqueries, and common table expressions, unless TOP, OFFSET or FOR XML is also specified.

I put a sniffer on the Sql server, and found this script being run:

SELECT COUNT(*)
FROM (
    SELECT DISTINCT [t].[ThingId], [t.ThingExtended].[ThingId], [t.ThingExtended].[SomeValue]
    FROM [dbo].[Thing] AS [t]
    LEFT JOIN [dbo].[ThingExtended] AS [t.ThingExtended] ON [t].[ThingId] = [t.ThingExtended].[ThingId]
    ORDER BY [t].[ThingId]
) AS [t]

... which, aside from being invalid Sql, is incorrect logically, since I only want the "distinct" modifier to apply to SomeValue.

@rowanmiller rowanmiller added this to the 2.0.0 milestone Jan 17, 2017
@maumar
Copy link
Contributor

maumar commented Jan 21, 2017

Looks like a duplicate of #7348 which is now fixed and will ship in 1.1.1

@maumar
Copy link
Contributor

maumar commented Jan 21, 2017

Unfortunately the fix is not ideal - distinct and count will now be applied on the client. This will again be fixed once we stop materializing unnecessary columns in the LEFT JOIN scenarios

@maumar
Copy link
Contributor

maumar commented Apr 8, 2017

This is now fixed, closing

@maumar maumar closed this as completed Apr 8, 2017
@maumar maumar added the closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. label Apr 8, 2017
@ajcvickers ajcvickers changed the title Wrong Sql emitted when doing a .Distinct() query on a one-to-one join Query: Wrong Sql emitted when doing a .Distinct() query on a one-to-one join May 9, 2017
@divega divega added closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. and removed closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. labels May 10, 2017
@ajcvickers ajcvickers modified the milestones: 2.0.0-preview1, 2.0.0 Oct 15, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. type-bug
Projects
None yet
Development

No branches or pull requests

5 participants