efcore icon indicating copy to clipboard operation
efcore copied to clipboard

The behavior of Distinct in SqlServer and Cosmos is inconsistent

Open Varorbc opened this issue 2 years ago • 8 comments

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

Varorbc avatar Nov 05 '21 01:11 Varorbc