1

Same question as this one but I need to remove objects with a combination of duplicate of two properties from List.

There is a set of objects, objects have age and Name:

21 Carl 23 Vladimir 25 Bob 21 Olivia 21 Carl 30 Jacob 23 Vladimir 

Output list should contain:

21 Carl 23 Vladimir 25 Bob 21 Olivia 30 Jacob 

How do I remove it?

3

3 Answers 3

1

Try this:

 public class KeyValueClass { public int Age { get; set; } public string Name { get; set; } } private void DoTheJob() { var myList = new List<KeyValueClass> { new KeyValueClass {Age = 21, Name = "Carl"}, new KeyValueClass {Age = 23, Name = "Vladimir"}, new KeyValueClass {Age = 25, Name = "Bob"}, new KeyValueClass {Age = 21, Name = "Olivia"}, new KeyValueClass {Age = 21, Name = "Carl"}, new KeyValueClass {Age = 30, Name = "Jacob"}, new KeyValueClass {Age = 23, Name = "Vladimir"}, }; var myDistinctList = myList.GroupBy(x => new { x.Age, x.Name }) .Select(c => c.First()).ToList(); } 
Sign up to request clarification or add additional context in comments.

Comments

1

You can use Distinct() from the Linq namespace and a IEqualityComparer:

class Program { static void Main(string[] args) { List<KeyValueClass> myList = new List<KeyValueClass> { new KeyValueClass {Age = 21, Name = "Carl"}, new KeyValueClass {Age = 23, Name = "Vladimir"}, new KeyValueClass {Age = 25, Name = "Bob"}, new KeyValueClass {Age = 21, Name = "Olivia"}, new KeyValueClass {Age = 21, Name = "Carl"}, new KeyValueClass {Age = 30, Name = "Jacob"}, new KeyValueClass {Age = 23, Name = "Vladimir"}, }; var myDistincList = myList.Distinct(new KeyValueEqualityComparer()); foreach (var item in myDistincList) { Console.WriteLine("Age: {0}, Name:{1}", item.Age, item.Name); } Console.ReadKey(); } } public class KeyValueClass { public int Age { get; set; } public string Name { get; set; } } class KeyValueEqualityComparer : IEqualityComparer<KeyValueClass> { public bool Equals(KeyValueClass x, KeyValueClass y) { if (x == null || y == null) return false; if (x.Age == y.Age && x.Name.Equals(y.Name)) return true; return false; } public int GetHashCode(KeyValueClass obj) { return (obj.Age + obj.Name).GetHashCode() + 387; } } 

Comments

0

Just use an ISet to avoid the overhead and slow performance of a List:

public class Person : IEquatable<Person> { public int Age { get; private set;} public string Name { get; private set;} public bool override Equals(Person other){ return other.Age == Age && other.Name.Equals(Name); } public override int GetHashCode(){ return Age.GetHashCode() ^ Name.GetHashCode(); } } private IEnumerable<Person> MakeUniqueList(IEnumerable<Person> input){ return new HashSet<Person>(input); } 

In order to actually remove, which will be quite slow performance wise (but it will save on memory usage), use List.Remove(T) https://learn.microsoft.com/en-us/dotnet/api/system.collections.generic.list-1.remove?view=netframework-4.7.2

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.