-1

I understand that Distinct() returns distinct elements from a sequence by using the default equality comparer to compare values. But I don't quite understand how this works in Entity Framework.

For example, if I have:

return dbContext.Products.Select(p => p.Dealer).Distinct();

How would the generated SQL decide if a Dealer equals another Dealer? Does it compare all the columns, or do something else?

Jonathan Wood
  • 65,341
  • 71
  • 269
  • 466
  • Why would it work differently with the Entity Framework? It works like described in https://learn.microsoft.com/en-us/dotnet/api/system.linq.enumerable.distinct?view=net-5.0 and you can overwrite the `Equals()` method. – Tony Stark May 16 '21 at 17:51
  • 1
    @TonyStark: Because the default comparer compares the address of each object, and that has no meaning in SQL. Also, you cannot override `Equals()` in SQL. – Jonathan Wood May 16 '21 at 17:53
  • Does this answer your question? [Entity Framework 6 query with Distinct filter](https://stackoverflow.com/questions/37595253/entity-framework-6-query-with-distinct-filter) – Riwen May 16 '21 at 18:08

1 Answers1

3

In LINQ Distinct() is mapped to SELECT DISTINCT Col1, Col2, Col3,....,

Which is semantically different than in IEnumerable.Distinct(). But the other options are simply useless, and this query pattern in SQL is occasionally useful.

I would typically expect to see it in queries like

var col = db.Products.Select(p => new {p.Color, p.Size, p.Material}).Distinct();

And anonymous types have a built-in property-wise comparer, so the in-memory implementation and the SQL translation agree.

David Browne - Microsoft
  • 80,331
  • 6
  • 39
  • 67