Entity framework is unique.

.net c# distinct entity-framework linq


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.

I tried with something like this:

List<post> posts = postsFromDatabase.Distinct().ToList();

But it doesn't work!

So I wrote this method in order to avoid the duplicates:

public List<Post> PostWithOutDuplicates(List<Post> posts)
        List<Post> postWithOutInclude = new List<Post>();
        var noDupes = posts.Select(x => x.Id).Distinct();
        if (noDupes.Count() < posts.Count)
            foreach (int idPost in noDupes)
                postWithOutInclude.Add(posts.Where(x => x.Id == idPost).First());
            return postWithOutInclude;
            return posts;

Any ideas of how to improve the performance??

Thanx in advance.

11/16/2012 10:04:08 AM

Accepted Answer

This is nice and easy:

List<Post> posts = posts
.GroupBy(x => x.Id)
.Select(x => x.FirstOrDefault())

But if you want to write it the proper way, I'd advise you to write it like this:

public class PostComparer : IEqualityComparer<Post>
    #region IEqualityComparer<Post> Members

    public bool Equals(Post x, Post y)
        return x.Id.Equals(y.Id);

    public int GetHashCode(Post obj)
        return obj.Id.GetHashCode();


As it will give you more freedom when it comes to additional comparisons. having written this class you can use it like this:

List<Post> posts = postsFromDatabase.Distinct(new PostComparer()).ToList();
12/16/2011 5:14:56 PM

Popular Answer

I think that write your own custom comparer is a good approach.

Here is an article in msdn that explains the topic very well: http://support.microsoft.com/kb/320727

The reason that the Distinct are not working its that Distinct() has no idea about how to detemine if there are equals, so it's using the reference to determine it it's the same "object". It's working like it's suposed to work. All the classes in the query are not the same object.

By writing your own comparer (it's easy) you can tell to Distinct() how to make the comparation to determine if they are equals.

Edit: If not using Distinct isn't a problem and the situation isn't frecuent, the first answer of Piotr Justyna it's simple and effective.

Related Questions


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow