I have an object that has a member of a list of another object. i.e:
public class ObjA
{
public int Id { get; set; }
Other members....
public List<ObjB> ObjBs{ get; set; }
}
public class ObjB
{
public int Id { get; set; }
Other members...
}
I'm currently populating ObjA object within a foreach, then within the foreach loop calling another foreach populating the list of ObjB and adding it to the ObjBs member within ObjA.
Ideally I'd like a flat list of ObjA with all associated ObjBs (by ID) appended to the list. Is this possible, I've got temp lists within my page of all ObjA's and all ObjB's, just not sure on how to join them into a dynamic single flat list?
Hope this makes sense?
Well it'll have to be just a List<object>
, but this should work:
// Flat list of ObjB
var referenced = listA.SelectMany(x => x.ObjBs);
// Concatenate listA with the flat list of ObjB
var flat = listA.Concat<object>(referenced).ToList();
See more on this question at Stackoverflow