The behavior of Distinct in SqlServer and Cosmos is inconsistent

Open
#26,547 8 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
25/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Stale
Tech stack
csharp, sql
Domain
backend, databases

Research direction

Start by running the supplied LINQ OrderBy(...).Distinct().ToQueryString() reproduction against both Microsoft.EntityFrameworkCore.SqlServer and Microsoft.EntityFrameworkCore.Cosmos at the reported EF Core version. Compare the generated SQL with the documented Distinct behavior, then determine the intended cross-provider result. Done means the behavior is aligned or the provider difference is clearly documented and covered by appropriate tests.

Written by the indexing model from the issue text.

Description

area-cosmos area-query customer-reported needs-design

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
[Table("Table_1")]
public partial class Table1
{
    [Key]
    public int Id { get; set; }

    public DateTime CreateTime { get; set; }
}

public partial class TestContext : DbContext
{
    public TestContext()
    {
    }

    public TestContext(DbContextOptions<TestContext> options)
        : base(options)
    {
    }

    public virtual DbSet<Table1> Table1 { get; set; }
}

var services = new ServiceCollection();
services.AddDbContext<TestContext>();
var serviceProvider = services.BuildServiceProvider();

var testContext = serviceProvider.GetRequiredService<TestContext>();
var sql = testContext.Table1.OrderBy(a => a.Id).Distinct().ToQueryString();
Console.WriteLine(sql);

Sql Server Behavior
SELECT DISTINCT [t].[Id], [t].[CreateTime]
FROM [Table_1] AS [t]
Cosmos Behavior
SELECT DISTINCT c
FROM root c
WHERE (c["Discriminator"] = "Table1")
ORDER BY c["Id"]
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

Dominant language
C#
Stars
14.8k
Forks
3.4k
Avg merge
2d 5h
Merged PRs (30d)
134

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from dotnet/efcore

All issues in dotnet/efcore

Similar issues

More C# issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.