title: Linq to Entities, Master Detail into DataContracts Query
body: <p>This is part of my WebAPI, and I'm having trouble getting this data out of Linq to entities into my Datacontract objects. I will be returning a custom data type to the caller, whether they want it XML or JSON, I don't care, I'm just handing this to WebAPI to take care of it. </p>
  
  <p>I'm using VS2013 and EF5, ASP.NET 4.5 in C#</p>
  
  <p>The structure is as follows:</p>
  
  <p><strong>ProductCategory</strong></p>
  
  <pre><code>ID
  CategoryName
  (a million other things)
  List&lt;Products&gt;
  </code></pre>
  
  <hr>
  
  <p><strong>Products</strong></p>
  
  <pre><code>ID
  ProductName
  (a million other things)
  Category
  </code></pre>
  
  <hr>
  
  <p>I have set up a DataContract that looks like the following:</p>
  
  <p><strong>ProductCategoryDataContract</strong></p>
  
  <pre><code>ProductCategoryId
  ProductCategoryName
  List&lt;ProductDataContract&gt; Products
  </code></pre>
  
  <hr>
  
  <p><strong>ProductDataContract</strong></p>
  
  <pre><code>ProductName
  </code></pre>
  
  <hr>
  
  <p>Basically, I want to get a Linq query to return ALL categories, and within it ALL products.</p>
  
  <pre><code>from prodcat in context.ProductCategories order by prodcat.ItemOrder
  select new ProductCategoryDataContract
  {
  ProductCategoryId = prodcat.Id
  Products = prodcat.Products // this obviously fails as ProductDataContract != Products
  }
  </code></pre>
  
  <p>If I try </p>
  
  <pre><code>Products = new List&lt;ProductDataContract&gt; { //initializer stuff }
  </code></pre>
  
  <p>I don't have any of the intellisense things I would think I would have (ProductName, etc), because I'm guessing I'm in the list. </p>
  
  <p>Basically, I have all the relationships set up and I can get everything in straight up EF, but because I'm putting these into new datacontracts, it's giving me a little grief (mostly due to my lack of linq knowledge).</p>
  
  <p>My question is:
  1.) how can I do this</p>
  
  <p>and 
  2.) how can I do this with minimal database hits. Potentially I'm firing off thousands of items within tens of product groups. </p>
  
  <p>Thanks much, and if I'm not clear on anything, please lmk. And, the above is pseudocodish, not the real deal so if I made stupid naming errors, that's unlikely 'it' :)</p>