Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linq: Select First Items from the Child List?

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?

like image 228
Amitabh Avatar asked Dec 22 '22 01:12

Amitabh


1 Answers

parents.Where(p => p.Children.Any()).Select(p => p.Children.First());
like image 90
Reddog Avatar answered Dec 29 '22 17:12

Reddog