If I am creating a viewmodel to pass to a view and my viewmodel is comprised of several properties from the same object, how should I create the LINQ query? At the moment I'm doing it like this:
TaskEditViewModel viewModel = new TaskEditViewModel
{
Task = taskRepository.Tasks.FirstOrDefault(t => t.Id == id),
Status = (taskRepository.Tasks.FirstOrDefault(t => t.Id == id).CompletionDate.ToString() == "") ? "close" : "open",
Account = taskRepository.Tasks.FirstOrDefault(t => t.Id == id).Accounts.First()
};
return View(viewModel);
My taskRepository returns IQueryable, so does this mean I am making 3 seperate db calls? Should I be making one call and then building the viewmodel from the results?
CompletionDate
andAccounts
are included in yourTask
model (isn't it always in LinqToSql?) so you could expose them as properties in the viewModel instead of setting them explicitly. – Ingó Vals Jan 11 '12 at 12:15