c# - Linq chained SELECT and DISTINCT operators -


i new linq , had basic query.

say have large list of customer objects

list<customer> c = null; c = //fetch db - resulting 1000+ non-unique customers; 

and if convert list list of class - lack of better name - customerentity , pick out distinct ones follows:

    var ce = c.select(cust => new customerentity()                       {                          customerid = cust.custid,                          customername = cust.custname                      }).distinct(new customerentitycomparer()).tolist(); 

customerentitycomparer class compares 2 customerentity objects on basis of customerid. query is: if select , distinct chained, result in multiple iterations on list?

thanks

vikas

to give more elaborate answer:
can notice select() returns ienumerable, , distinct(). that's because you're creating query. no selection or distinct filtering done until call tolist() method. when tolist() method executed, whole query evaluated. that's called deferred execution.

the advantage of can create queries like:

var cequery = c.select(cust => new customerentity()  {     customerid = cust.custid,     customername = cust.custname }).distinct(new customerentitycomparer()); 

and every time "c" has changed, can re-use same cequery latest restul:

var ce = cequery.tolist(); 

Comments

Popular posts from this blog

PHPMotion implementation - URL based videos (Hosted on separate location) -

javascript - Using Windows Media Player as video fallback for video tag -

c# - Unity IoC Lifetime per HttpRequest for UserStore -