Select all columns but group by only one in linq
<p>I have been looking for a way to get multiple columns but group by only one in SQL and I found some info. However I can not came up with a way to do it in linq.</p>

<p>I have the following toy example table:</p>

<pre><code>| Id | Message | GroupId | Date |
|-------------------------------|
| 1  | Hello   | 1       | 1:00 |
| 2  | Hello   | 1       | 1:01 |
| 3  | Hey     | 2       | 2:00 |
| 4  | Dude    | 3       | 3:00 |
| 5  | Dude    | 3       | 3:01 |
</code></pre>

<p>And I would like to recover all columns for the rows that have a distinct <code>GroupId</code> as follows (with a 'Date' desc order):</p>

<pre><code>| Id | Message | GroupId | Date |
|-------------------------------|
| 1  | Hello   | 1       | 1:00 |
| 3  | Hey     | 2       | 2:00 |
| 4  | Dude    | 3       | 3:00 |
</code></pre>

<p>I do not really care about which row is picked from the grouped ones (first, second...) as long as is the only one given that group Id. </p>

<p>I have came out with the following code so far but it does not do what is supposed to:</p>

<pre><code>List&lt;XXX&gt; messages = &lt;MyRep&gt;.Get(&lt;MyWhere&gt;)
                            .GroupBy(x =&gt; x.GroupId)
                            .Select(grp =&gt; grp.OrderBy(x =&gt; x.Date))
                            .OrderBy(y =&gt; y.First().Date)
                            .SelectMany(y =&gt; y).ToList();
</code></pre>