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 ofdatelistfirst class, ofdatelistsecond class, etc)tolist()creates concretelist<datetime>data- the
var tempdateliststatic typed, , inferslist<datetime>expression
Comments
Post a Comment