title: Entity MVC and ViewModels one to many example
body: <p>I have a simple example</p>
  
  <p>I want to display a list of companies and employees</p>
  
  <pre><code>ACME
      David
      Simon
      Zac
  Microsoft
      Kevin
      Paul
  </code></pre>
  
  <p>Currently I do a linq query that returns the company model then traverse the model tree to get the employees. This works fine, however using glimpse I see its doing in the above example 3 queries. One for company then 2 queries for employees one for each company.</p>
  
  <p>Im wondering if I would be better off having a ViewModel representing the data and populating that. However when trying to build the view model I dont know how to build it without doing multiple queries.</p>
  
  <pre><code>var data =
      from company in db.Companies
      join employee in db.Employees on company.companyId equals employee.companyId
      orderby company.name, employee.name
      select new { companyName = company.name, employeeName = employee.name }
  </code></pre>
  
  <p>Is there an easy way to link this to the view model or is there another best practice way of doing something simple like this.</p>