title: Asp.net LINQ groupby and orderBy on dates does not retrieve the expected output
body: <p>I am working on an asp.net mvc3 application with linq2sql.</p>
  
  <p>I have a List of SiteLog object type containing also for each object: A string named CLRExceptionType and a date named EntryDate. This List is included in a Dictionary:</p>
  
  <pre><code>private Dictionary&lt;string, List&lt;SiteLog&gt;&gt; dataBaseList = new Dictionary&lt;string, List&lt;SiteLog&gt;&gt;();
  DataClasses1DataContext db = new DataClasses1DataContext("string1");
  DataClasses1DataContext db2 = new DataClasses1DataContext("string2");
  </code></pre>
  
  <p>And I populate the vocabulary in the same controller in a function:</p>
  
  <pre><code>   private void CreateDictionary()
  {   
      dataBaseList.Add("db", db.SiteLogs.ToList());
      dataBaseList.Add("db2", db2.SiteLogs.ToList());
  }
  </code></pre>
  
  <p>Then I have this Linq query:</p>
  
  <pre><code>var result =
  dataBaseList.GroupBy(x =&gt; x.Key)
            .SelectMany(x =&gt;
                          x.SelectMany(n =&gt; n.Value
                                             .GroupBy(g =&gt; g.CLRExceptionType)
                                             .Select(g =&gt; new
                                                          {
                                                            DB = x.Key,
                                                            Exception = g.Key,
                                                            Count = g.Count(), 
                                                            LastOccured = 
                                                             g.Max(y =&gt;
                                                                   y.EntryDate)
                                                          })))
            .OrderBy(x =&gt; x.DB)
            .ThenByDescending(x =&gt; x.LastOccured);
  </code></pre>
  
  <p>That should give me the following output: <br></p>
  
  <p>Database1:<br>
  Exception1 (22 times) last occurred: 22.10.1989 19:30<br>
  Exception2 (2 times) last occurredd 20.5.1980 14.50<br>
  <br>
  Database2<br>
  Exception1 (22 times) last occurred: 21.10.1989 19:30<br>
  Exception2 (2 times) last occurredd 20.5.1980 14.50<br></p>
  
  <p>Then suddenly the List is updated with a new entry in the database2 so I have to put this database on top:
  <br>
  Database2<br>
  Exception1 (1 times) last occurred: 29.07.2011 12.00<br>
  Exception1 (22 times) last occurred: 21.10.1989 19:30<br>
  Exception2 (2 times) last occurredd 20.5.1980 14.50<br>
  <br>
  Database1:<br>
  Exception1 (22 times) last occurred: 22.10.1989 19:30<br>
  Exception2 (2 times) last occurredd 20.5.1980 14.50<br></p>
  
  <p>So basically, I can order the logs in the database By date, but I cannot order the databases by the last date occurred on it, how can I solve this?
  Viewcode (Index.cshtml):</p>
  
  <pre><code>&lt;div id="results"&gt;
  
  @{
      string firstTime = "";
  }
   @foreach( var database in Model)
   {
         int currentCol = 0 ; 
          if (!(firstTime == database.DB))
          {
            &lt;br style="clear:both" /&gt;&lt;h3&gt; @database.DB &lt;/h3&gt;
                  currentCol = 0;
          }
  
             &lt;div class="logContainer" onclick="location.href='/logs/Details?databaseID=@database.DB&amp;exceptionName=@database.Exception&amp;exceptionsOccurred=@database.Count';"&gt;
  
                  @if (database.Count &gt; 999)
                  {
                      &lt;div class="counter-small"&gt;&lt;b&gt;@database.Count&lt;/b&gt;&lt;/div&gt;
                  }
                  else
                  { 
                  &lt;div class="counter"&gt;&lt;b&gt;@database.Count&lt;/b&gt;&lt;/div&gt; 
                  }
                  &lt;div class="exceptionName"&gt; Exceptions of Type: @database.Exception&lt;/div&gt;
                  &lt;div class="date"&gt;Siste: @database.LastOccurred&lt;/div&gt;
           &lt;hr /&gt;   &lt;/div&gt;
       currentCol += 1;
            if (currentCol == 2) { //3 columns were displayed, switch row
      currentCol = 0;
      &lt;br style="clear:both" /&gt;
  }
  
       firstTime = database.DB; 
  }
  
  &lt;/div&gt;
  </code></pre>
  
  <p>Tnx in advance</p>