Click here to Skip to main content
15,889,852 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
var result = (from r in db.Routes
                         join tin in db.TripIns on r.RouteId equals tin.RouteId
                         join tout in db.TripOuts on r.RouteId equals tout.RouteId
                         select new
                         {
                             RouteId = r.RouteId,
                             RouteName = r.RouteName,
                             RouteDesc = r.RouteDesc,
                             tripins = new TripIn{ TripInId = tin.TripInId, TripInName = tin.TripInName },
                            tripouts = new TripOut { TripOutId = tout.TripOutId, TripOutName = tout.TripOutName }

                         }).ToList()
                     .Select(x => new Route()
                     {
                         RouteId = x.RouteId,
                         RouteName = x.RouteName,
                         RouteDesc = x.RouteDesc,
                         TripIns = x.tripins,
                         TripOuts = x.tripouts
                     });


What I have tried:

var result = (from r in db.Routes
                         join tin in db.TripIns on r.RouteId equals tin.RouteId
                         join tout in db.TripOuts on r.RouteId equals tout.RouteId
                         select new
                         {
                             RouteId = r.RouteId,
                             RouteName = r.RouteName,
                             RouteDesc = r.RouteDesc,
                             tripins = new TripIn{ TripInId = tin.TripInId, TripInName = tin.TripInName },
                            tripouts = new TripOut { TripOutId = tout.TripOutId, TripOutName = tout.TripOutName }

                         }).ToList()
                     .Select(x => new Route()
                     {
                         RouteId = x.RouteId,
                         RouteName = x.RouteName,
                         RouteDesc = x.RouteDesc,
                         TripIns = x.tripins,
                         TripOuts = x.tripouts
                     });
Posted
Updated 9-Feb-17 1:30am

1 solution

Try putting the ToList at the end of the command. Change:
    }).ToList()
.Select(x => new Route()
{
    RouteId = x.RouteId,
    RouteName = x.RouteName,
    RouteDesc = x.RouteDesc,
    TripIns = x.tripins,
    TripOuts = x.tripouts
});
To:
    })
.Select(x => new Route()
{
    RouteId = x.RouteId,
    RouteName = x.RouteName,
    RouteDesc = x.RouteDesc,
    TripIns = x.tripins,
    TripOuts = x.tripouts
}).ToList();
 
Share this answer
 
Comments
Richard Deeming 9-Feb-17 7:34am    
If the Route class isn't part of the database model, you might need an .AsEnumerable() between the two .Select(...) calls.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900