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;
}
else
{
return posts;
}
}
Any ideas of how to improve the performance??
Thanx in advance.
C# Linq Distinct() method removes the duplicate elements from a sequence (list) and returns the distinct elements from a single data source. It comes under the Set operators' category in LINQ query operators, and the method works the same way as the DISTINCT directive in Structured Query Language (SQL).
The distinct keyword is used in conjunction with select keyword. It is helpful when there is a need of avoiding duplicate values present in any specific columns/table. When we use distinct keyword only the unique values are fetched. column1, column2 : Names of the fields of the table.
The SELECT DISTINCT statement is used to return only distinct (different) values. Inside a table, a column often contains many duplicate values; and sometimes you only want to list the different (distinct) values.
The distinct keyword is used with select keyword in conjunction. It is helpful when we avoid duplicate values present in the specific columns/tables. The unique values are fetched when we use the distinct keyword. SELECT DISTINCT returns only distinct (different) values.
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();
}
#endregion
}
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();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With