c# - Selecting object with collection of objects from many-to-many relationship -


i have 3 tables: athletes, teams, , team_athletes. team_athletes joins other 2 tables in many-to-many relationship. i'm doing querying database return object athlete , collection of teams. i'm doing following 2 queries:

   var query = (from in db.athletes                 join ta in db.team_athletes on a.id equals ta.athleteid                 join t in db.teams on ta.teamid equals t.id                 t.organizationid == organizationid                  orderby a.lastname, a.firstname                 select new                 {                     athlete = a,                     team = t                 }).toarray();     var result = in query                 group i.athlete g                 select new                  {                     athlete = g.first().athlete,                     teams = g.select(i => i.team).toarray()                 }; 

i'd know how combine queries if possible, can't come works. thoughts?

why don't use:

var query = in db.athletes             join ta in db.team_athletes on a.id equals ta.athleteid             join t in db.teams on ta.teamid equals t.id             t.organizationid == organizationid              orderby a.lastname, a.firstname             group t g             select new { athlete = g.key, teams = g }; 

you iterate through result, so:

foreach (var entry in query) {     console.writeline("athlete: {0}", entry.athlete);     foreach (team t in entry.teams)     {         console.writeline("team: {0}", t);     } } 

Comments

Popular posts from this blog

c++ - No viable overloaded operator for references a map -

java - Custom OutputStreamAppender not run: LOGBACK: No context given for <MYAPPENDER> -

java - Cannot secure connection using TLS -