title: Distinct in Entity framework
body: <p>I have a List of objects that some of them have the same Ids, so I would like to remove those elements that are duplicated. </p>
  
  <p>I tried with something like this:</p>
  
  <pre><code>List&lt;post&gt; posts = postsFromDatabase.Distinct().ToList();
  </code></pre>
  
  <p>But it doesn't work!</p>
  
  <p>So I wrote this method in order to avoid the duplicates:</p>
  
  <pre><code>public List&lt;Post&gt; PostWithOutDuplicates(List&lt;Post&gt; posts)
      {
          List&lt;Post&gt; postWithOutInclude = new List&lt;Post&gt;();
          var noDupes = posts.Select(x =&gt; x.Id).Distinct();
          if (noDupes.Count() &lt; posts.Count)
          {
              foreach (int idPost in noDupes)
              {
                  postWithOutInclude.Add(posts.Where(x =&gt; x.Id == idPost).First());
              }
              return postWithOutInclude;
          }
          else
          {
              return posts;
          }
      }
  </code></pre>
  
  <p>Any ideas of how to improve the performance??</p>
  
  <p>Thanx in advance.</p>