c# - Combine list<object> and list<otherobject> into single flat list -


i have object has member of list of 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 populating obja object within foreach, within foreach loop calling foreach populating list of objb , adding objbs member within obja.

ideally i'd flat list of obja associated objbs (by id) appended list. possible, i've got temp lists within page of obja's , objb's, not sure on how join them dynamic single flat list?

hope makes sense?

edit

based on comment, looks want:

ienumerable<object[]> output = lista.select(a => (new []{a}).cast<object>()                                                             .concat(a.objbs)                                                              .toarray()); 

or

var output = lista.select(a => (new [] {new {a.id}})                                           .concat(a.objbs.select(b => new {b.id}));                              ); 

original answer

here's one-liner, although it's not in order specified (if matters):

ienumerable<object> output = lista.selectmany(a => a.objbs                                                     .cast<object>()                                                     .concat(new [] {a}) ); 

or if want create anonymous type has common property(ies):

var output = lista.selectmany(a => a.objbs.select(b => new {b.id})                                           .concat(new [] {new {a.id}})                              ); 

that way can @ least access common properties without casting or reflection.


Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

Python ctypes access violation with const pointer arguments -