title: Select distinct on one column only using Entity Framework Code First
body: <p>I'm developing an Entity Framework Code First library using C# and .NET Framework 4.0.</p>
  
  <p>I have this table on my database:</p>
  
  <p><img src="https://i.stack.imgur.com/XbWCd.png" alt="enter image description here"></p>
  
  <p><code>SentBy</code> and <code>SentTo</code> columns are FK to a User table.</p>
  
  <p>I want to get users that have sent a message to another user, but I only want to get it once. To do it, I use this SQL Statement:</p>
  
  <pre><code>select distinct SentBy, DateUtcSent
    from Messages where isAFriendshipRequest = 0 and SentTo = 15
  order by DateUtcSent DESC;
  </code></pre>
  
  <p>I do <code>order by DateUtcSent DESC;</code> to get the latest message sent first.</p>
  
  <p>The idea is to get a row for each user that has sent a message to user 15.</p>
  
  <p>But I get this:</p>
  
  <p><img src="https://i.stack.imgur.com/ZdmAt.png" alt="enter image description here"></p>
  
  <p>As you can see, I get user 2 twice.</p>
  
  <p>I don't need the last row.</p>
  
  <p>Here, I'm trying to get each different user that has sent a message to user 15, and get user id, dateUtcSent and the body on the result.</p>
  
  <p><strong>How can I do it to don't get a sentBy value more than once?</strong></p>
  
  <h2><strong>UPDATE</strong></h2>
  
  <p>And, If I want to to do the this:</p>
  
  <pre><code>select distinct SentTo, DateUtcSent
    from Messages where isAFriendshipRequest = 0 and SentBy = 15
  order by DateUtcSent DESC;
  </code></pre>
  
  <p>And then, merge both results without repeating users and getting the last message.</p>
  
  <p><strong>How can I do it?</strong></p>