title: LINQ Distinct on a particular property and latest
body: <p>Suppose I have the following collection</p>
  <pre><code>public class User
  {
      public string SSN { get; set; }
      public DateTime StartDate { get; set; }
  }
  
  var users = new List&lt;User&gt;
  {
      new User {  SSN = &quot;ab&quot;, StartDate = new DateTime(2021, 01, 01) },
      new User {  SSN = &quot;ab&quot;, StartDate = new DateTime(2021, 01, 02) }, // take this
  
      new User {  SSN = &quot;ac&quot;, StartDate = new DateTime(2021, 01, 01) },
      new User {  SSN = &quot;ac&quot;, StartDate = new DateTime(2021, 02, 01) }, // take this
  
      new User {  SSN = &quot;ad&quot;, StartDate = new DateTime(2020, 01, 01) },
      new User {  SSN = &quot;ad&quot;, StartDate = new DateTime(2021, 01, 01) },
      new User {  SSN = &quot;ad&quot;, StartDate = new DateTime(2022, 01, 01) }, // take this
  };
  </code></pre>
  <p>What I am trying to do is to get SSN distinct but by only latest StartDate and I created two queries which seems to work. There is a better way in term of perfomance?</p>
  <pre><code>// shows only latest is selected
  var district = users
      .OrderByDescending(p =&gt; p.StartDate)
      .GroupBy(g =&gt; g.SSN)
      .Select(x =&gt; x.First())
      .ToList();
  
  var ssn = users
      .OrderByDescending(p =&gt; p.StartDate)
      .GroupBy(g =&gt; g.SSN)
      .Select(x =&gt; x.First())
      .Select(x=&gt; x.SSN)
      .ToList();
  </code></pre>