IQueryable.Distinct() vs List.Distinct()

前端 未结 1 620
感情败类
感情败类 2021-01-19 01:53

I have a linq query that I am using Distinct() on. If I just call Distinct() without converting to a List then it does not return a distinct list -

相关标签:
1条回答
  • 2021-01-19 02:22

    Obviously you cannot have exact duplicate rows (including the primary key) in your table. Probably what you mean is rows with some equal fields (excluding primary key).

    Calling Distinct on IQueryable, generates a SQL DISTINCT operator on the resulting query, which compares every field of your table against each other. Because you cannot have exact duplicate rows in the table, it returns all the rows.

    On the other hand, calling Distinct on a List<User> will use Equals method of the User object to compare objects in memory (after fetching all the rows from database). The final result depends on the implementation of Equals method, which could check only some fields for equal values.

    0 讨论(0)
提交回复
热议问题