I have the following one-to-many relation between two objects.
Parent
--> IList<Child>
Now, I have a List of Parent objects and I want the First Child of each parent in the list.
What is the best way to do this using Linq?
I have the following one-to-many relation between two objects.
Parent
--> IList<Child>
Now, I have a List of Parent objects and I want the First Child of each parent in the list.
What is the best way to do this using Linq?
parents.Where(p => p.Children.Any()).Select(p => p.Children.First());
You can iterate each "Parent" and find the first of it's offspring:
parent.FirstOrDefault(child => parent.Children.First());