-
Notifications
You must be signed in to change notification settings - Fork 3.2k
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: Translate Distinct to SQL when applied after custom projection #7234
Comments
Based on documentation of
For anonymous type in C#,
Select Clause in T-Sql
For database, the rows are the same if all columns have same values. |
Thanks for the response! You are right, but when working with a DbContext, I personally think of Distinct(IQueryable) as a way to force a distinct to be executed on the server(query provider). I'm sure that a lot of people are used to this way of query construction. Also, if you run the following using EF 6 context.Blogs
.Select(t => new BlogDto
{
Url = t.Url
})
.Distinct()
.ToList(); you will get the following translation: SELECT
[Distinct1].[C1] AS [C1],
[Distinct1].[Url] AS [Url]
FROM ( SELECT DISTINCT
[Extent1].[Url] AS [Url],
1 AS [C1]
FROM [dbo].[Blogs] AS [Extent1]
) AS [Distinct1] which is the same query as the one I mentioned in the workaround and again I think a lot of people are used to this mindset when constructing a query. It's just confusing for me that somehow the server should be aware of comparing c# classes. I saw the following
at https://docs.microsoft.com/en-us/ef/efcore-and-ef6/ Even more, if you run: context.Blogs
.Distinct()
.ToList(); you will get SELECT DISTINCT [b].[BlogId], [b].[Url]
FROM [Blogs] AS [b] and I didn't find an override of the Equals method in the DbSet. |
EF Team Triage: We discussed this and we believe we should try to support this query and the translation to |
I think I may have addressed this at least somewhat with #7543: |
@tuespetre - If you think the PR resolves this thing then can you please add a test case. |
@smitpatel the linked spot in the diff shows two applicable tests, looks like they would need to be renamed |
I looked up for this problem and didn't find anything. Sorry if I'm making a dup or I messed up something.
This
resulted in the following query:
and I expected:
So, now I'm using the following workaround:
which results in:
Steps to reproduce
Just to be clear, the following works as expected:
produces:
I have one simple question regarding terminology. When you say 'projection' ('projecting' to some object different from the model), does that include anonymous objects? If so, the title of this issue should be "Projecting to a specific object(class) with Distinct is not working as expected"
Thank you!
Further technical details
EF Core version: 1.1.0
Database Provider: Microsoft.EntityFrameworkCore.SqlServer
Operating system: Win 8.1 64bit
IDE: Visual Studio 2015 Update 3
The text was updated successfully, but these errors were encountered: