title: Entity Framework Projecting Multiple Models to Single DTO
body: <p>I want to ask how to map multiple model objects to a single DTO.</p>
  
  <p>I have the following EF models as follows:</p>
  
  <p><strong>Bracket.cs</strong></p>
  
  <pre><code>namespace MyApp.Models
  {
      using System;
      using System.Collections.Generic;
      using System.ComponentModel.DataAnnotations;
      using System.ComponentModel.DataAnnotations.Schema;
      using System.Data.Entity.Spatial;
  
      [Table("Bracket")]
      public partial class Bracket
      {
          [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
          public Bracket()
          {
              Teams = new HashSet&lt;Team&gt;();
          }
  
          public int BracketId { get; set; }
  
          [Required]
          [StringLength(50)]
          public string BracketName { get; set; }
  
          [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
          public virtual ICollection&lt;Team&gt; Teams { get; set; }
      }
  }
  </code></pre>
  
  <p><strong>Team.cs</strong></p>
  
  <pre><code>namespace MyApp.Models
  {
      using System;
      using System.Collections.Generic;
      using System.ComponentModel.DataAnnotations;
      using System.ComponentModel.DataAnnotations.Schema;
      using System.Data.Entity.Spatial;
  
      [Table("Team")]
      public partial class Team
      {
          public int TeamId { get; set; }
  
          [Required]
          [StringLength(50)]
          public string TeamName { get; set; }
  
          public int BracketId { get; set; }
  
          public virtual Bracket Bracket { get; set; }
      }
  }
  </code></pre>
  
  <p>And I created DTO objects to represent them.</p>
  
  <pre><code>namespace MyApp.Models
  {
      public class BracketDto
      {
          public int BracketId { get; set; }
  
          public string BracketName { get; set; }
  
          public List&lt;TeamDto&gt; Teams { get; set; }
      }
  
      public class TeamDto
      {
          public int TeamId { get; set; }
  
          public string TeamName { get; set; }
  
          public int BracketId { get; set; }
      }
  }
  </code></pre>
  
  <p>In one of my class I use to query the database and project the resulting data, this is what I am doing:</p>
  
  <pre><code>public List&lt;BracketDto&gt; GetBrackets()
          {
              var brackets = (from a in this.Context.Brackets
                              join b in this.Context.Teams on a.BracketId equals b.BracketId
                              select new BracketDto()
                              {
                                  BracketId = a.BracketId,
                                  BracketName = a.BracketName
                              }).ToList&lt;BracketDto&gt;();
  
              var teams = (from a in this.Context.Teams
                           select new TeamDto()
                           {
                               TeamId = a.TeamId,
                               TeamName = a.TeamName,
                               BracketId = a.BracketId
                           }).ToList&lt;TeamDto&gt;();
  
              foreach(BracketDto bracket in brackets)
              {
                  var tl = teams.Where(a =&gt; a.BracketId == bracket.BracketId).ToList();
                  bracket.Teams = tl;
              }
  
              var xtra = (from a in this.Context.Brackets
                          join b in this.Context.Teams on a.BracketId equals b.BracketId
                          select new BracketDto()
                          {
                              BracketId = a.BracketId,
                              BracketName = a.BracketName
                          }).ToList&lt;BracketDto&gt;();
  
              return brackets;
          }
  </code></pre>
  
  <p>Where I am getting results:</p>
  
  <pre><code>[
      {
          "BracketId": 1,
          "BracketName": "Eastern Conference",
          "Teams": [
              {
                  "TeamId": 1,
                  "TeamName": "Indiana Pacers",
                  "BracketId": 1
              },
              {
                  "TeamId": 3,
                  "TeamName": "Cleveland Cavaliers",
                  "BracketId": 1
              }
          ]
      },
      {
          "BracketId": 2,
          "BracketName": "Western Conference",
          "Teams": [
              {
                  "TeamId": 2,
                  "TeamName": "Los Angeles Lakers",
                  "BracketId": 2
              },
              {
                  "TeamId": 4,
                  "TeamName": "Los Angeles Clippers",
                  "BracketId": 2
              }
          ]
      }
  ]
  </code></pre>
  
  <p>I know that there could be a more efficient way to do this. But I am quite unsure how to do it on the LINQ query I have.</p>
  
  <p>Looking for advise and suggestions. Thank you!</p>