title: How to aggregate more than two entities using Linq for a SelectMany?
body: <p>I have a database with multiple tables that I am trying to "flatten" into one table on another database. I am able to easily do this with Entity Framework using only two tables (or entities), but trying to add in 3 or more gives me compile errors. There's got to be a way to do this, right? I feel like I am not formatting my expression correctly. Please look at how I have it set up with two entities below:</p>
  
  <pre><code>var flatTable= DB1.MainTable.SelectMany(mt =&gt; DB1.SecondaryTable.Where(st =&gt; st.MainID == mt.MainID),  (mt, st) =&gt; new FlatTable
  {
      MainID = mt.MainID,
      AgeLessThan1 = st.AgeLessThan1,
      Age1to4 = st.Age1to4,
      Age5to19 = st.Age5to19,
      Age20to49 = st.Age20to49,
      AgeGreaterThanEqual50 = st.AgeGreaterThanEqual50,
      AgeUnknown = st.AgeUnknown
  }).ToList();
  </code></pre>
  
  <p>The above code successfully gets data from the two tables on the first database and creates new aggregate rows to be inserted into the "flat" table on the other database. My issue is that this entity, <code>FlatTable</code> that is being created has many more fields that need to be populated from other tables that I can't seem to add within this chunk of code. Is what I am asking possible, or will I have to add in the fields from each additional table in multiple steps?</p>
  
  <p>Below is the code for adding the finished aggregate data to the new table on the second database:</p>
  
  <pre><code>foreach (flatRow row in flatTable)
  {
      T2.AddToFlatTable(row);
  }
  T2.SaveChanges();
  </code></pre>