-
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
The behavior of Distinct in SqlServer and Cosmos is inconsistent #26547
Comments
@Varorbc providers aren't necessarily meant to behave in the same way, around this question or around others - it's perfectly OK to have different behaviors across providers when the database itself warrants that. As discussed in #25696, the LINQ documentation for the Distinct operator clearly specifies that "the result sequence is unordered". And as discussed in that issue, one main issue in relational databases is that Distinct frequently causes queries to be pushed down into subqueries, but in SQL, subqueries return unordered resultsets; so if we can't guarantee ordering after Distinct in some/most cases, we generally shouldn't in any case (otherwise behavior is inconsistent depending on whether there's a SQL subquery or not). I don't know if the same is true for Cosmos or not - it's a different database with different behavior; for one thing, uncorrelated subqueries aren't supported at all. So we'd have to investigate the Cosmos behavior more thoroughly to see what makes sense there. Having said all that, I do agree this is something we should look into. |
can you explain more or a sample ? and follow sql work perfectly for sqlserver SELECT DISTINCT <tb_filed> FROM <table> ORDER BY <tb_filed> DESC |
@John0King see #25696, this is discussed there (e.g. #25696 (comment)) |
@John0King note that I'm discussing subqueries |
😄 I am looking for a sample that do not work with discussing and subquerys, so I can understand you more clearly. so far , it seems > var hashSet = new HashSet<int>();
. for (var i = 0; i < 10; i++)
. {
. hashSet.Add(i);
. }
.
. hashSet.Remove(2);
. hashSet.Add(1000);
.
. hashSet.OrderBy(x=>x).Distinct().ToArray()
// int[10] { 0, 1, 3, 4, 5, 6, 7, 8, 9, 1000 }
> hashSet.OrderByDescending(x=>x).Distinct().ToArray()
// int[10] { 1000, 9, 8, 7, 6, 5, 4, 3, 1, 0 } |
@John0King please fully read #25696, this has already been discussed there. Even if in LINQ to Objects Distinct "happens" to return ordered items (at least in some cases), the documentation clearly specifies that this isn't in the contract, so it could change. |
Related info: https://docs.microsoft.com/en-us/azure/cosmos-db/sql/sql-query-keywords#distinct
|
Note to self: check specifically the behavior of ordering in the inner query (which contains distinct) as well as the outer query. |
https://docs.microsoft.com/en-us/dotnet/api/system.linq.queryable.distinct?view=net-6.0
https://docs.microsoft.com/en-us/ef/core/what-is-new/ef-core-6.0/whatsnew#distinct-queries
Include your code
Sql Server Behavior
Cosmos Behavior
Include provider and version information
EF Core version:6.0.0-rc.2.21480.5
Database provider: Microsoft.EntityFrameworkCore.SqlServer/Microsoft.EntityFrameworkCore.Cosmos
Target framework: .NET 6.0
Operating system:Win11
IDE: Visual Studio 2022 17.0
The text was updated successfully, but these errors were encountered: