linq - EF Code First oddity with Distinct() -


there don't understand code bellow. i'm trying find workers have duplicate module in modules collection.

here's entities (simplified sake of brevity):

public class worker {     public int id { get; private set; }     public icollection<takentrainingmodule> takentrainingmodules { get; private set; }      public worker()     {         takentrainingmodules = new hashset<takentrainingmodule>();     } }  public class takentrainingmodule {     public int id { get; set; }     public int trainingmoduleid { get; set; } } 

and here's query:

var query = worker in _context.workers.include(worker => worker.takentrainingmodules)             let distinctmodules = worker.takentrainingmodules.select(module => module.trainingmoduleid).distinct()             worker.takentrainingmodules.count != distinctmodules.count()             select worker; 

with query bellow, returned workers have takentrainingmodules collection empty.

but, next query (without using keywork let), collection , correctly loaded:

var query = worker in _context.workers.include(worker => worker.takentrainingmodules)             worker.takentrainingmodules.count != worker.takentrainingmodules.select(module => module.trainingmoduleid).distinct().count()             select worker; 

what missing? let keyword first executing distinct query , fools object state manager module loaded aren't , selector doesn't load them next?

any explanations welcome! :-)

i've updated query this:

var query = worker in _context.workers             let distinctmodules = worker.takentrainingmodules.select(module => module.trainingmoduleid).distinct()             worker.takentrainingmodules.count != distinctmodules.count()             select worker;  return query.include(worker => worker.takentrainingmodules).toarray(); 

and fine. dismissile pointing me out nice answer.


Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -