views:

60

answers:

3

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?

+7  A: 
parents.Where(p => p.Children.Any()).Select(p => p.Children.First());
Reddog
A: 

You can iterate each "Parent" and find the first of it's offspring:

parent.FirstOrDefault(child => parent.Children.First());
VoodooChild
A: 

parent.child.FirstOrDefault();

jocoteco