Is there a way for these foreach loops to only have one lambda expression?
private int getNextEventId() { int numOfEvents = 0; foreach (MonthModel eventMonth in eventsForAllMonths) { foreach (DayModel eventDay in eventMonth.AllDays) { numOfEvents += eventDay.CalEvents.Count; } } return numOfEvents + 1; }
int numOfEvents = eventsForAllMonths.SelectMany(m => m.AllDays) .Select(d => d.CalEvents.Count) .Sum();
return (eventsForAllMonths.SelectMany(eventMonth => eventMonth.AllDays).Sum(eventDay => eventDay.CalEvents.Count) + 1);
Try the following:
private int getNextEventId() { int numOfEvents = eventsForAllMonths.SelectMany(eventMonth => eventMonth.AllDays).Aggregate(0, (current, eventDay) => current + eventDay.CalEvents.Count); return numOfEvents + 1; }
How about this?
return (from m in eventsForAllMonths from d in m.AllDays select d.CalEvents.Count).Sum() + 1;
I ForEach()for life, therefore:
ForEach()
eventsForallMonths.ForEach(em=>em.ForEach(ed=>numOfEvents+=ed.CalEvents.Count));
How to convert this to lambda?
List<Item> listItem = new List<Item>(); foreach (StackPanel sp in spPhotos.Children) { foreach(Item item in sp.Children) { listItem.Add(item); } sp.Children.Clear(); } spPhotos.Children.Clear();