How to select last record in a LINQ GroupBy clause
<p>I have the following simple table with <em>ID</em>, <em>ContactId</em> and <em>Comment</em>.</p>

<p><a href="https://i.stack.imgur.com/4WFKQ.png" rel="noreferrer"><img src="https://i.stack.imgur.com/4WFKQ.png" alt="enter image description here"></a></p>

<p>I want to select records and <code>GroupBy contactId</code>. I used this <code>LINQ</code> extension method statement:</p>

<pre><code>Mains.GroupBy(l =&gt; l.ContactID)
 .Select(g =&gt; g.FirstOrDefault())
 .ToList()
</code></pre>

<p>It returns record <code>1</code> and <code>4</code>. How can I use <code>LINQ</code> to get the <code>ContactID</code> with the highest <code>ID</code>? (i.e. return <code>3</code> and <code>6</code>)</p>