c# - List is empty after linq operation -
i have problem understend happening in foreach loop- listofbookedtimes successfuly gets elements want, after execution of next line listofbookedtimes empty. why? (all lists contain datetime)
foreach (var day in alldays) { list = rep.getlistofworkinghours(fulldayworkinghours, day, spworkinghours); bookedtimes = _bookingsservice.getbookedtimes(day, providerid); foreach (var b in bookedtimes) { var listofbookedtimes = list.where(m => m.timeofday == (b.timeofappointment.timeofday)); list.removeall(m => m.timeofday == (b.timeofappointment.timeofday)); listofbookedtimes.select(m => m.year - 50); list.addrange(listofbookedtimes); }
your problem not removeall
rather fundemental understanding of linq , yield return.
when call
list.where(m => m.timeofday == (b.timeofappointment.timeofday));
it not executed rather returns enumerator filter collection on iteration. in next line remove entries wanted fetch in previous line.
when iterate collection in
list.addrange(listofbookedtimes);
it empty.
solution: add .toarray()
or .tolist()
after where
, should work expected. this:
var listofbookeditems = list.where(m => m.timeofday == (b.timeofappointment.timeofday)) .tolist();
Comments
Post a Comment