Move the entities out of your database first, in that manner you only issue one query request:
// apply any filter(s) needed here.
var orderList = orders.ToList();
// then do injecting using the "cached" orders
var orderViewModel = orderList
.Select(x => new OrderViewModel().InjectFrom(x))
.Cast<OrderViewModel>()
.ToList();
// then inject into your details model
var detailsModel = orderList
.Select(x => new _DetailsViewModel().InjectFrom(x))
.Cast<_DetailsViewModel>()
.ToList();
And a small suggestion if I may, remove the underscore for _DetailsViewModel
to make the naming standard.
UPDATE:
How do I add the detailsModel to the orderViewModel afterwards to pass to the Order View?
You just simply set it to the instance of OrderViewModel
like so:
orderViewModel.Details = detailsModel ;
Then return orderViewModel
to your view and do your thing there:
@Html.Partial("_OrderDetails", Model.Details)