c# - Retrieving a single column from LINQ joins -


i have few database tables (could not include original, made up: db schema

here sample of data be: example

the idea fighter can ranger can have weapon named "fire bow " has spell type of "bow fire". user can select "bow fire" spell want use @ time.

also, magician can wizard can wield "frozen wand" weapon has spell type of "wand ice". user can select "wand ice" spell they want use @ time.

suppose want know unique magic damage types selected classes. instance, magic damage types can healer , fighter use? in t-sql, this:

select distinct mag.name class c join subclass s on c.id = s.classid join subclassweapon w on s.id = w.subclassid join magicdamagetype mag on w.magicdamagetypeid = mag.id 

in example above, give me 2 results (given fighter , magician): "bow fire" , "wand ice".

i'm trying result using linq. able retrieve selected list of classes , tried this:

classlist.select(s => s.subclasses.select(s => s.subclassweapons.select(s => s.magicdamagetype.name))).tolist(); 

i got funky nested string list result. there way in linq without using foreach loops? i'm looking list of strings magicdamagetype names come query.

thanks!

you're looking selectmany

classlist.selectmany(s => s.subclasses)          .selectmany(s => s.subclassweapons)          .select(s => s.magicdamagetype.name).tolist(); 

here's example independent of yours

void main() {     blog myblog = new blog();      myblog.posts           .selectmany(post => post.comments)           .select(comment => comment.id)           .tolist()           .foreach(console.writeline); }  public class blog {     public blog()     {         posts = new list<post>         {             new post(),             new post(),             new post(),         };     }      public list<post> posts { get; set; } }  public class post {     public post()     {         comments = new list<comment>         {             new comment(),             new comment(),         };     }     public list<comment> comments { get; set; } }  public class comment {     public comment()     {         this.id = guid.newguid().tostring("n");     }     public string id { get; set; }      public override string tostring()     {         return this.id;     } }