title: Mapping Linq Query results to a DTO class
body: <p>I want to get records from the database using EF and assign the values to a DTO class.Consider the following tables for a Linq query.</p>
  
  <p>TableA,TableB, TableC</p>
  
  <p>For each TableA record there are multiple records in TableB. For each TableB record there are multiple records in TableC. 
  Now my DTOs look like this</p>
  
  <pre><code>public class TableA_DTO
  {
      public int tableA_rowid { get; set; }
      //remaining tableA field definitions
  
      public List&lt;TableB_DTO&gt; TableB_records { get; set; }
  }
  
  public class TableB_DTO
  {
      public int tableB_rowid { get; set; }
      //remaining tableB  field definitions
  
      public List&lt;TableC_DTO&gt; TableC_records { get; set; }
  }
  
  public class TableC_DTO
  {
      public int tableC_rowid { get; set; }
      //remaining tableC field definitions
  }
  </code></pre>
  
  <p>my linq query looks something like this </p>
  
  <pre><code>var qry = from ent in TableA
            select ent;
  </code></pre>
  
  <p>In my mapping class I loop through items in query result like so:</p>
  
  <pre><code>    foreach (var dataitem in query)
      {
          TableA_DTO dto = new TableA_DTO();
          dto.tableA_rowid =  dataitem.ID;
          //remaining field definitions here
      }
  </code></pre>
  
  <p>Now this works for all fields in TableA where it brings out one record from the database and sets the required properties in TableA_DTO for each field in the table TableA. I want to also populate all matching records in TableB in the TableA property field by the name TableB_records and also in TableB_DTO all the matching records from TableC in TableB_DTO's property by the name TableC_records </p>
  
  <p>Can this be done? What do I need to change? Is it the linq query or the way I do my mapping</p>
  
  <p>Thanks for your time...</p>