title: Linq projection into DTO objects
body: <p><strong>TL;DR:</strong> The code should show my intent, the place where it goes wrong is in the output statement, where I'm trying to set the StatusItemDTOs, which should contain all the fields from the anonymous objects, except LocationName.</p>
  
  <hr>
  
  <p>I'm trying to query my db and extract only the fields I need using projection. 
  The intended result is a LocationDTO wrapping StatusItemDTOs. The hard part is mapping over multiple DTOs in one query.</p>
  
  <p>Where it goes wrong is in the output statement where I'm trying to set the StatusItemDTOs, which I have no idea how to do out from the dictionary values.
  If you look at my DTO classes in bottom of this post, you can see that the StatusItemDTO contains all the fields of the anonymous objects, except LocationName. The only reason I'm making the anonymous objects is because I don't know how to "store" the LocationName if I'm just selecting new StatusItemDTOs.</p>
  
  <p>I feel pretty confident the query can be done shorter, and smarter, but I'm inexperienced with projection into DTOs and hope you can help.</p>
  
  <pre><code>var query = (from liq in Context.LocationItemQuantities
             where liq.Location.DepotId == depotId
             select new
             {
               LocationName = liq.Location.Name,
               ItemTypeName = liq.ItemType.Name,
               DepotQuantity = liq.Quantity,
               StandardQuantity = liq.StandardQuantity,
               MinimumQuantity = liq.MinQuantity,
             }).ToList();
  
          var output = from anon in query
                       group anon by anon.LocationName into g
                       select new LocationDTO
                       {
                           LocationName = g.Key,
                           StatusItemDTOs = g
                       };
  </code></pre>
  
  <p>My DTOs:</p>
  
  <pre><code>public class StatusItemDTO
  {
      public int DepotQuantity { get; set; }
      public string ItemTypeName { get; set; }
      public DateTime ExpirationDate { get; set; }
  
      public int StandardQuantity { get; set; }
      public int? MinimumQuantity { get; set; }
  }
  
  public class LocationDTO
  {
      public List&lt;StatusItemDTO&gt; StatusItemDTOs { get; set; }
      public string LocationName { get; set; }
  }
  </code></pre>
  
  <p>Edit: Entity classes</p>
  
  <pre><code> public class LocationItemQuantity : IEntity
      {
          public int Id { get; set; }
  
          public int Quantity { get; set; }
  
          public int? MinQuantity { get; set; }
  
          public int StandardQuantity { get; set; }
  
          public bool IsChecked { get; set; }
  
          public int LocationId { get; set; }
  
          public Location Location { get; set; }
  
          public int ItemTypeId { get; set; }
  
          public ItemType ItemType { get; set; }
  
      }
  
  public class Location : IEntity
      {
          public int Id { get; set; }
  
          public List&lt;LocationItemQuantity&gt; LocationItemQuantities { get; set; }
  
          public string Name { get; set; }
  
          public int? DepotId { get; set; }
  
          public Depot Depot { get; set; }
  
      }
  
   public class ItemType : IEntity
  {
      public int Id { get; set; }
  
      public string InternBarcode { get; set; }
  
      public string Description { get; set; }
  
      public string Name { get; set; }
  
      public List&lt;LocationItemQuantity&gt; LocationItemQuantities { get; set; }
  
      public List&lt;ProductBatch&gt; ProductBatches { get; set; }
  
      public List&lt;Product&gt; Products { get; set; }
  
  }
  </code></pre>