-4

I have 3 collection : teams, routes, waypoints

Teams may have many routes, routes may have many waypoints

I want take collection of teams, every item of team must have collection routes, every route must have collection waypoints.

I know how is do for two collections. It is my query

var waypoints = from team in teams
                join route in Routes on team.TeamID equals route.TeamID
                into rList
                select new { teamName= team.teamName, Rlist = rList};
foreach (var t in waypoints)
{
    Console.WriteLine("<{0} team>:", t.teamName);
    foreach (var m in t.Rlist)
        Console.WriteLine(" " + m.RouteName);
    Console.WriteLine();
}

How I Can connect third join WayPoints ?

I want to get like this result

{   
    team_1-> route_1->waypoint_1
                      waypoint_3
                      waypoint_5
    team_2-> route_2->waypoint_4      
                      waypoint_5    
                      waypoint_9       
    team_4-> route_5->waypoint_5
                      waypoint_7
                      waypoint_8
}
qqqqq
  • 11
  • 2
  • This looks like a copypasted code, it doesn't make any sense – Marco Salerno Oct 27 '17 at 12:50
  • 2
    https://stackoverflow.com/help/mcve If you could give us a complete code sample (including sample inputs in the code, and well defined expected outputs) we can better help you. – mjwills Oct 27 '17 at 12:51
  • 1
    Possible duplicate of [How to join 3 tables with linq](https://stackoverflow.com/questions/41933985/how-to-join-3-tables-with-linq) – Renatas M. Oct 27 '17 at 12:53
  • 2
    If you have foreign key relationships properly setup in EF then you can do this with Navigation properties instead of joins https://coding.abel.nu/2012/06/dont-use-linqs-join-navigate/ – juharr Oct 27 '17 at 12:56
  • Your picture doesn't express a valid C# object - if you expressed your object properly, (try a class definition) I think it would help you understand. – NetMage Oct 27 '17 at 20:47

1 Answers1

0

You need to nest your LINQ to produce the answers desired:

var waypoints = from team in teams
                join route in Routes on team.TeamID equals route.TeamID into rList
                select new {
                    team.teamName,
                    Rlist = (from route in rList
                             join waypoint in WayPoints on route.RouteID equals waypoint.RouteRouteID into wList
                             select new {
                                 route,
                                 Wlist = wList.ToList()
                             }).ToList()

};

NetMage
  • 26,163
  • 3
  • 34
  • 55