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