Reputation: 287
I have a list object, lets call it "Parent". Within Parent a have several "Child" objects. Within "Child", I have a DateTime property called StartDay.
I would like to run a linq query that will examine Parent and return the day of the week of StartDay if it is always the same day of the week. If for example we have some StartDay's on wednesday and some on tuesday it will return "Variable".
Upvotes: 0
Views: 37
Reputation: 7095
parents.Where(x => x.Children.Any())
.Select(x => new { Parent = x, StartDays = x.Children.Select(y => y.StartDay).Distinct() })
.Select(x => new { x.Parent, StartDay = x.StartDays.Count() == 1 ? x.StartDays.Single().ToString() : "Variable" });
Upvotes: 0
Reputation: 460380
If you want to check if all parents' children have the same StartDay.DayOfWeek
:
bool allOnSameDay = Parent.All(p => {
var firstChildDayOfWeek = p.Children.First().StartDay.DayOfWeek;
return p.Children.All(c => c.StartDay.DayOfWekk == firstChildDayOfWeek);
})
Upvotes: 1