I have a collection of items which each contain a secondary collection. I need to generate a list of new items based off the total set of secondary items. Eg if you have a list of dogs and each dog has a list of friends, I need a total list of all dog friends for all dogs.
Normally I would do this with SelectMany but the difficulty here is my secondary objects do not contain a reference back to the parent object, and the new list needs to contain fields from both secondary and parent objects. ie as below
var dogs = List<Dog>
var newList = dogs.SelectMany(i => i.DogFriends).Select(dogFriend => new NewObject
{
fieldOne = dog.propertyOne,
fieldTwo = dog.propertyTwo
fieldThree = dogFriend.propertyOne
});
In the above, I have no reference to dog however, so I cannot select those.
What I'm looking for is a way to do a join on the two tables as in SQL, so you can select values from both objects. But I want the total values to match the total number of child objects. Ie if you have 3 dogs with 3 friends each, the final list will have 9 objects.
How can I achieve this with LINQ?