title: LINQ - Nested Query
body: <p>I have a SQL Statement that I am trying to convert to a LINQ query. I need to do this because I can't edit my database :(. Regardless, I have a SQL Statement that looks like the following:</p>
  
  <pre><code>SELECT
    CustomerID,
    FirstName,
    LastName,
    Gender,
    BirthMonth,
    (SELECT COUNT(ID) FROM PurchaseOrder WHERE [CustomerID]=CustomerID) as TotalPurchases
  FROM
    MyEntities
  </code></pre>
  
  <p>I know how to do everything in LINQ excepted for the nested query part. Currently, I have the following:</p>
  
  <pre><code>var results = from x in context.MyEntities 
                select new Customer() 
                { 
                  CustomerID = x.CustomerID, 
                  FirstName = x.FirstName, 
                  LastName = x.LastName, 
                  Gender = x.Gender, 
                  BirthMonth = x.BirthMonth,
                  TotalPurchases = ? 
                };
  </code></pre>
  
  <p>How do I execute a nested query in LINQ to get the value for TotalPurchases?</p>
  
  <p>Thank you so much!</p>