c# - IQueryable.Distinct() vs List.Distinct() -


i have linq query using distinct() on. if call distinct() without converting list not return distinct list - still contains duplicates.

however if convert list , then call distinct() - works expected , unique objects.

i'm using telerik orm , objects being returned class representing 1 of tables in database.

var uniqueusers = (from u in database.users                     select u).distinct(); 

the code above not produce distinct results, when convert list , call distinct - does:

var uniqueusers = (from u in database.users                     select u).tolist().distinct(); 

i suspect has collection before being converted list, comparing references objects rather object data not understand going on - why fist code example not produce unique results , happens collection when using .tolist() makes work?

[edit] i've simplified above queries, in real world query has several joins generates non-unique results, returning user objects.

i tried overriding equals , gethashcode methods did not make difference.

public override bool equals(object obj) {     user comparingobject = obj user ;      if (comparingobject == null)     {         return false;     }     else     {         return comparingobject.userid.equals(this.userid);     } }  public override int gethashcode() {     return this.userid.gethashcode(); } 

[update] having run same query in linqpad, works expected providing list of distinct entries. running same query in linqpad when using telerik orm dll multiple entries. appears peculiarity telerik. when have time investigate further , raise telerik support.

obviously cannot have exact duplicate rows (including primary key) in table. mean rows equal fields (excluding primary key).

calling distinct on iqueryable, generates sql distinct operator on resulting query, compares every field of table against each other. because cannot have exact duplicate rows in table, returns rows.

on other hand, calling distinct on list<user> use equals method of user object compare objects in memory (after fetching rows database). final result depends on implementation of equals method, check fields equal values.


Comments

Popular posts from this blog

java - nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet Hibernate+SpringMVC -

sql - Postgresql tables exists, but getting "relation does not exist" when querying -

asp.net mvc - breakpoint on javascript in CSHTML? -