Search code examples
c#entity-frameworklinqdistinct

Understanding how Distinct() works with Entity Framework


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?


Solution

  • 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.