title: How to use LINQ to group by and order by certain column
body: <p><strong>UPDATED</strong>: added a new column combining the Date and Time column which are string fields into DateTimeCombined column which is a DateTime field</p>
  
  <p>So what the LINQ should do is group by name column and get the row for each name that has the earliest date+time. Then it should add the rest of the row for the name.</p>
  
  <p>DataTable init:</p>
  
  <pre><code>dataT = new DataTable();
  dataT.Columns.Add("Date", typeof(string));
  dataT.Columns.Add("Time", typeof(string));
  dataT.Columns.Add("Day", typeof(string));
  dataT.Columns.Add("Name", typeof(string));
  dataT.Columns.Add("Place", typeof(string));
  dataT.Columns.Add("DateTimeCombined", typeof(DateTime));
  dataT.Columns.Add("NameMessage", typeof(string));
  </code></pre>
  
  <p>So here is the starting DataTable (which is retrieved by default):</p>
  
  <pre><code>Date        Time        Day     Name        Place       DateTimeCombined            NameMessage
  6/29/2017   8:30AM      MON     John        Orlance     6/29/2017 8:30:00 AM
  6/29/2017   8:40AM      MON     John        Orlance     6/29/2017 8:40:00 AM
  6/29/2017   8:50AM      MON     John        Orlance     6/29/2017 8:50:00 AM        
  6/29/2017   9:10AM      MON     John        Orlance     6/29/2017 9:10:00 AM
  6/29/2017   9:20AM      MON     John        Orlance     6/29/2017 9:20:00 AM
  6/29/2017   1:00PM      MON     John        Orlance     6/29/2017 1:00:00 PM
  6/30/2017   8:30AM      TUE     John        Orlance     6/30/2017 8:30:00 AM
  6/30/2017   8:40AM      TUE     John        Orlance     6/30/2017 8:40:00 AM
  6/29/2017   8:15AM      MON     Mike        Atlanta     6/29/2017 8:15:00 AM
  6/29/2017   8:30AM      MON     Mike        Atlanta     6/29/2017 8:30:00 AM
  6/29/2017   8:40AM      MON     Mike        Atlanta     6/29/2017 8:40:00 AM
  6/29/2017   9:10AM      MON     Mike        Atlanta     6/29/2017 9:10:00 AM
  6/29/2017   9:20AM      MON     Mike        Atlanta     6/29/2017 9:20:00 AM
  6/30/2017   8:30AM      TUE     Mike        Atlanta     6/30/2017 8:30:00 AM
  6/30/2017   8:40AM      TUE     Mike        Atlanta     6/30/2017 8:40:00 AM
                                  Christine   Marion                                  None
                                  Steph       Kearney                                 None
  6/29/2017   8:30AM      MON     Jenny       Boise       6/29/2017 8:30:00 AM
  6/29/2017   8:40AM      MON     Jenny       Boise       6/29/2017 8:40:00 AM
  6/29/2017   8:50AM      MON     Jenny       Boise       6/29/2017 8:50:00 AM
  6/29/2017   9:10AM      MON     Jenny       Boise       6/29/2017 9:10:00 AM
  6/29/2017   9:20AM      MON     Jenny       Boise       6/29/2017 9:20:00 AM
  6/30/2017   8:30AM      TUE     Jenny       Boise       6/30/2017 8:30:00 AM
  6/30/2017   8:40AM      TUE     Jenny       Boise       6/30/2017 8:40:00 AM
  6/29/2017   8:30AM      MON     Kelly       Ardsley     6/29/2017 8:30:00 AM
  6/29/2017   8:40AM      MON     Kelly       Ardsley     6/29/2017 8:40:00 AM
  6/29/2017   8:50AM      MON     Kelly       Ardsley     6/29/2017 8:50:00 AM
  6/29/2017   9:10AM      MON     Kelly       Ardsley     6/29/2017 9:10:00 AM
  6/29/2017   9:20AM      MON     Kelly       Ardsley     6/29/2017 9:20:00 AM
  6/30/2017   8:30AM      TUE     Kelly       Ardsley     6/30/2017 8:30:00 AM
  6/30/2017   8:40AM      TUE     Kelly       Ardsley     6/30/2017 8:40:00 AM
                                  Joseph      Houston                                 None
  </code></pre>
  
  <p>The first LINQ function should be to get the earlier <code>DateTimeCombined</code> for each Name:</p>
  
  <pre><code>6/29/2017   8:30AM      MON     John        Orlance     6/29/2017 8:30:00 AM
  6/29/2017   8:15AM      MON     Mike        Atlanta     6/29/2017 8:15:00 AM
  6/29/2017   8:30AM      MON     Jenny       Boise       6/29/2017 8:30:00 AM
  6/29/2017   8:30AM      MON     Kelly       Ardsley     6/29/2017 8:30:00 AM
  </code></pre>
  
  <p>The next function should be to order that by <code>DateTimeCombined</code>:</p>
  
  <pre><code>- If `DateTimeCombined` is same, order first by `DateTimeCombined` and then by Name.
  
  - If `DateTimeCombined` is same AND Name is same, order first by `DateTimeCombined` and then by Name and then by Place.
  
  
  6/29/2017   8:15AM      MON     Mike        Atlanta     6/29/2017 8:15:00 AM
  6/29/2017   8:30AM      MON     Jenny       Boise       6/29/2017 8:30:00 AM
  6/29/2017   8:30AM      MON     John        Orlance     6/29/2017 8:30:00 AM
  6/29/2017   8:30AM      MON     Kelly       Ardsley     6/29/2017 8:30:00 AM
  </code></pre>
  
  <p>The next function should be to put the rest of the rows for each name (the final DataTable should look like this):</p>
  
  <pre><code>6/29/2017   8:15AM      MON     Mike        Atlanta     6/29/2017 8:15:00 AM
  6/29/2017   8:30AM      MON     Mike        Atlanta     6/29/2017 8:30:00 AM
  6/29/2017   8:40AM      MON     Mike        Atlanta     6/29/2017 8:40:00 AM
  6/29/2017   9:10AM      MON     Mike        Atlanta     6/29/2017 9:10:00 AM
  6/29/2017   9:20AM      MON     Mike        Atlanta     6/29/2017 9:20:00 AM
  6/30/2017   8:30AM      TUE     Mike        Atlanta     6/30/2017 8:30:00 AM
  6/30/2017   8:40AM      TUE     Mike        Atlanta     6/30/2017 8:40:00 AM
  6/29/2017   8:30AM      MON     Jenny       Boise       6/29/2017 8:30:00 AM
  6/29/2017   8:40AM      MON     Jenny       Boise       6/29/2017 8:40:00 AM
  6/29/2017   8:50AM      MON     Jenny       Boise       6/29/2017 8:50:00 AM
  6/29/2017   9:10AM      MON     Jenny       Boise       6/29/2017 9:10:00 AM
  6/29/2017   9:20AM      MON     Jenny       Boise       6/29/2017 9:20:00 AM
  6/30/2017   8:30AM      TUE     Jenny       Boise       6/30/2017 8:30:00 AM
  6/30/2017   8:40AM      TUE     Jenny       Boise       6/30/2017 8:40:00 AM
  6/29/2017   8:30AM      MON     John        Orlance     6/29/2017 8:30:00 AM
  6/29/2017   8:40AM      MON     John        Orlance     6/29/2017 8:40:00 AM
  6/29/2017   8:50AM      MON     John        Orlance     6/29/2017 8:50:00 AM        
  6/29/2017   9:10AM      MON     John        Orlance     6/29/2017 9:10:00 AM
  6/29/2017   9:20AM      MON     John        Orlance     6/29/2017 9:20:00 AM
  6/29/2017   1:00PM      MON     John        Orlance     6/29/2017 1:00:00 PM
  6/30/2017   8:30AM      TUE     John        Orlance     6/30/2017 8:30:00 AM
  6/30/2017   8:40AM      TUE     John        Orlance     6/30/2017 8:40:00 AM
  6/29/2017   8:30AM      MON     Kelly       Ardsley     6/29/2017 8:30:00 AM
  6/29/2017   8:40AM      MON     Kelly       Ardsley     6/29/2017 8:40:00 AM
  6/29/2017   8:50AM      MON     Kelly       Ardsley     6/29/2017 8:50:00 AM
  6/29/2017   9:10AM      MON     Kelly       Ardsley     6/29/2017 9:10:00 AM
  6/29/2017   9:20AM      MON     Kelly       Ardsley     6/29/2017 9:20:00 AM
  6/30/2017   8:30AM      TUE     Kelly       Ardsley     6/30/2017 8:30:00 AM
  6/30/2017   8:40AM      TUE     Kelly       Ardsley     6/30/2017 8:40:00 AM
  </code></pre>
  
  <p>Note: If Kelly was John (John appearing twice), for example, then the Ardsley group would go before Orlance.</p>
  
  <p>What I tried so far:</p>
  
  <pre><code>var ordered = dataTable.AsEnumerable().OrderBy(en =&gt; en.Field&lt;DateTime&gt;("DateTimeCombined")).CopyToDataTable();
  </code></pre>
  
  <p>Update:</p>
  
  <pre><code>var ordered = dataTable.AsEnumerable()
                  .OrderBy(en =&gt; en.Field&lt;DateTime&gt;("DateTimeCombined"))
              .GroupBy(en1 =&gt; en1.Field&lt;string&gt;("Name")).ToList();
  </code></pre>
  
  <p>Just gives me the name only.</p>
  
  <p>Update:</p>
  
  <pre><code>  var q = dataTable.AsEnumerable()
                  .GroupBy(item =&gt; item.Field&lt;string&gt;("Name"))
                  .SelectMany(grouping =&gt; grouping.Take(1))
                  .OrderBy(item =&gt; item.Field&lt;DateTime&gt;("CombinedDateTime"))
                  .ThenBy(item =&gt; item.Field&lt;string&gt;("Name"))
                  .ThenBy(item =&gt; item.Field&lt;string&gt;("Place"))
                  .CopyToDataTable();
  </code></pre>
  
  <p>The above works as expected:</p>
  
  <pre><code>6/29/2017   8:15AM      MON     Mike        Atlanta     6/29/2017 8:15:00 AM
  6/29/2017   8:30AM      MON     Jenny       Boise       6/29/2017 8:30:00 AM
  6/29/2017   8:30AM      MON     John        Orlance     6/29/2017 8:30:00 AM
  6/29/2017   8:30AM      MON     Kelly       Ardsley     6/29/2017 8:30:00 AM
  </code></pre>
  
  <p>but only if i take the first row of each group (by Name). If I take all by group, the rows get all mixed up. How can I now, append the rest of <code>Mike's</code> rows after the row for Mike, append the rest of <code>Jenny's</code> rows after the row for Jenny, and so forth? Can this be done in the same LINQ?</p>