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

LINQ Join to Top(1) - expected SQL? #5754

Closed
agileorange opened this issue Jun 14, 2016 · 3 comments
Closed

LINQ Join to Top(1) - expected SQL? #5754

agileorange opened this issue Jun 14, 2016 · 3 comments

Comments

@agileorange
Copy link

agileorange commented Jun 14, 2016

Using EF Core RC2.

Issue as described in stackoverflow http://stackoverflow.com/questions/37810721/linq-join-to-top1?noredirect=1#comment63087615_37810721.

Two Entities: User and Photo. Photo has nullable UserId. So a User can have 0-n Photos.

To select all Users with the first Photo URI I have the following code.

var users = _context.Users.Select( x => new
       {
            Name= x.Name,
            PhotoUri= x.Photos.Select( c => c.Uri ).FirstOrDefault()
       } );

The SQL I would expect is this:

SELECT   User.Name, Photo.Uri
FROM     User
LEFT OUTER JOIN     Photo
ON       Photo.Id =
         (
         SELECT  TOP 1 Id 
         FROM    Photo
         WHERE   UserId = User.Id
         )

However EF is generating one SELECT of Users followed by multiple SELECTs on Photos, one for each User record.

The same occurs with the following code variation:

var users = _context.Users.Select( x => new
       {
            Name= x.Name,
            PhotoUri= x.Photos.FirstOrDefault().Uri
       } );

Is this what I should expect?

@anpete
Copy link
Contributor

anpete commented Jun 14, 2016

Looks like #4007

@agileorange
Copy link
Author

@anpete yes quite possibly.

@rowanmiller
Copy link
Contributor

Closing as dupe

@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
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants