c# - linq help for deep date list -


i have class list of 2nd class has list of dates. best way entire list of dates in top level class?

given following:

class 1 contains:  public list<class2> class2list;  class 2 contains:  list<datetime> datelist; 

what best way entire list of dates in class 1? know can loop through each of class 2 items , list way, i’m hoping there cleaner way linq.

list<datetime> tempdatelist;  foreach (var class2 in class2list) {   foreach (var dt in class2.datelist) {             tempdatelist.add(dt); }  }  return tempdatelist; 

pretty simple really;

var tempdatelist = class2list.selectmany(x => x.datelist).tolist(); 
  • selectmany(x => x.datelist) essentially performs inner loop here, creating continuous sequence (all of datelist first class, of datelist second class, etc)
  • tolist() creates concrete list<datetime> data
  • the var tempdatelist static typed, , infers list<datetime> expression

Comments

Popular posts from this blog

linux - Using a Cron Job to check if my mod_wsgi / apache server is running and restart -

actionscript 3 - TweenLite does not work with object -

jQuery Ajax Render Fragments OR Whole Page -