I have a list of objects which contains a list of other objects.
List<Parent> Ps;
Parent:
class Parent
{
List<Childs> Cs;
}
Is there a possibility to create with Linq a list of tuples of parents and childs?
Tuple<Parent, Child>
You can use Enumerable.SelectMany:
List<Tuple<Parent, Child>> parentChilds = Ps
.SelectMany(p => p.Cs.Select(c => Tuple.Create(p, c)))
.ToList();
This is equal to this:
var pcQuery = from parent in Ps
from child in parent.Cs
select Tuple.Create(parent, child);
List<Tuple<Parent, Child>> parentChilds = pcQuery.ToList();