I managed to put together following code to correctly Populate RequestExecutionViewModel model,however this looks extremely verbose to me. I feel some sort of LINQ builtin mapping construct can be used to reduce the over all code size. Please suggest improvements.string userName = User.Identity.Name;List<Requests> requests = db.Requests.Where(a => a.UserName.                          Equals(userName)).ToList();List<RequestExecutionViewModel> items = new List<RequestExecutionViewModel>();foreach (Requests request in requests) {   RequestExecutionViewModel requestview = new RequestExecutionViewModel();  requestview.Request = request;  requestview.ExecutionStatus = db.ExecutionStatus.Where(a =>    a.RequestId.Equals(request.RequestsId)).ToList();  requestview.Approvals = db.Approvals.Where(a =>  a.RulesId.Equals(request.RulesId)).ToList();  items.Add(requestview);}Edit: With the help of jessehouwing answer I restructured my models and removed the need for RequestExecutionViewModel entirely. Now my query is simplified to this.var requests = db.Requests.Include(r => r.Rule);
How to make this ASP.NET MVC ViewModel population query efficient?
c#
It's hard to tell exactly how to write this query without knowing exactly what your datamodel look like and how you've set it up. An assumption was made you're using Linq 2 SQL, I'll adhere to that assumption unless we hear otherwise. You can eagerly fetch other tables if you tell linq to sql to do so. You use the Include method for that.If you've set up your datamodel correctly, you should be able to create a query that resembles this:string userName = User.Identity.Name;List<RequestExecutionViewModel> = (from request in db.Requests.Include(Requests.ExecutionStatus).Include(Requests.Rules.Approvals);    where  request.UserName == userName    select new RequestExecutionViewModel(){        Request = request,        ExecutionStatus = request.ExecutionStatus,        Approvals = request.Rule.Approvals    }).ToList();And as you can see, this query instructs Linq-to-SQL to eagerly fetch the to related tables and then uses them in the output shaping process to create your viewmodel directly..Make sure you look at the profiler to see that this is actually resulting in one query. Linq-to-SQL can at any time decide to loop over the subresults by itself.