title: Multiple LINQ to SQL queries. Enumeration optimisation
body: <p>I have a list of ~ 15,000 'team's that need an individual linq query to return results.</p>
  
  <p>Namely - [Select the last 10 games for 'team']</p>
  
  <pre><code>public IEnumerable&lt;ResultsByDate&gt; SelectLast10Games(DateTime date, string team)
          {
              return
              (   from e in db.E0s
                  where e.DateFormatted &lt; date &amp;&amp;
                  (e.HomeTeam == team || e.AwayTeam == team)
                  orderby e.Date descending
                  select new ResultsByDate
                  {
                      Date = e.Date,
                      HomeTeam = e.HomeTeam,
                      AwayTeam = e.AwayTeam,
                      HomeGoals = e.FTHG,
                      AwayGoals = e.FTAG
                  }
              ).Take(10);
          }
  </code></pre>
  
  <p>This query is probably fine, it seems fast enough when called 15,000 times.</p>
  
  <p>My real issue is that I have to enumerate each query and this really kills the performance.</p>
  
  <p>For each of these queries I need to run a method on the 10 results and hence the queries need enumerating.</p>
  
  <p>The question is how can I avoid 15,000 enumerations? </p>
  
  <p>I thought about placing each of the results into a big list and <strong>then</strong> calling .ToList() or whatever's best, but adding to a List enumerates as it goes along so this doesn't seem viable.</p>
  
  <p>Is there a way to combine all 15,000 LINQ queries into one giant LINQ query such as..</p>
  
  <pre><code>public IEnumerable&lt;ResultsByDate&gt; SelectLast10Games(DateTime date, List&lt;string&gt; Teams)
          {
              foreach(var team in Teams)
              { var query = 
                    (from e in db.E0s
                     where e.DateFormatted &lt; date &amp;&amp;
                     (e.HomeTeam == team || e.AwayTeam == team)
                     orderby e.Date descending
                     select new ResultsByDate
                     {
                       Date = e.Date,
                       HomeTeam = e.HomeTeam,
                       AwayTeam = e.AwayTeam,
                       HomeGoals = e.FTHG,
                       AwayGoals = e.FTAG
                     }
                     ).Take(10);
              }
          }
  </code></pre>
  
  <p>So this would return one huge result set that I can then enumerate in one go and work from there?</p>
  
  <p>I have tried but I can't seem to get the LINQ loop correct ( if it's even possible - and the best way to fix my issue).</p>
  
  <p>The whole program takes ~ 29 minutes to complete. Without the enumeration its around 30 seconds which is not amazing but satisfactory given the criteria.</p>
  
  <p>Thanks!</p>