title: Using Linq to SQL, how do I find min and max of a column in a table?
body: <p>I want to find the fastest way to get the min and max of a column in a table with a single Linq to SQL roundtrip.  So I know this would work in two roundtrips:</p>
  
  <pre><code>int min = MyTable.Min(row =&gt; row.FavoriteNumber);
  int max = MyTable.Max(row =&gt; row.FavoriteNumber);
  </code></pre>
  
  <p>I know I can use <code>group</code> but I don't have a <code>group by</code> clause, I want to aggregate over the whole table!  And I can't use the .Min without grouping first.  I did try this:</p>
  
  <pre><code>from row in MyTable 
  group row by true into r 
  select new { 
      min = r.Min(z =&gt; z.FavoriteNumber), 
      max = r.Max(z =&gt; z.FavoriteNumber) 
  }
  </code></pre>
  
  <p>But that crazy group clause seems silly, and the SQL it makes is more complex than it needs to be.</p>
  
  <p>So, is there any way to just get the correct SQL out?</p>
  
  <p>EDIT: These guys failed too: <a href="https://stackoverflow.com/questions/1597181/linq-to-sql-how-to-aggregate-without-a-group-by">Linq to SQL: how to aggregate without a group by?</a> ... lame oversight by LINQ designers if there's really no answer.</p>
  
  <p>EDIT 2: I looked at my own solution (with the nonsensical constant group by clause) in the SQL Server Management Studio execution plan analysis, and it looks to me like it is identical to the plan generated by:</p>
  
  <pre><code>SELECT MIN(FavoriteNumber), MAX(FavoriteNumber)
  FROM MyTable
  </code></pre>
  
  <p>so unless someone can come up with a simpler-or-equally-as-good answer, I think I have to mark it as answered-by-myself.  Thoughts?</p>