Member 10062724 Ответов: 1

Как присвоить выбранные значения списку с помощью linq


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
                     });


Что я уже пробовал:

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
                     });

1 Ответов

Рейтинг:
1

OriginalGriff

Попробуйте поставить ToList в конце команды. Изменить:

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


Richard Deeming

Если Route класс не является частью модели базы данных, вам может понадобиться .AsEnumerable() между этими двумя .Select(...) звонки.