Click here to Skip to main content
Licence CPOL
First Posted 9 Nov 2009
Views 3,503
Downloads 26
Bookmarked 7 times

Ways to Remove from List by Property Value

By Henrik Sterndorff Jessen | 9 Nov 2009
Having 2 Lists, how do you remove from List1 all the instances of List2, matching a property value

1
1 vote, 100.0%
2

3

4

5
2.00/5 - 1 vote
μ 2.00, σa 5.00 [?]

Introduction

Having two generic lists (List<MyClass>), how do I remove from List1 all the instances of List2, matching a property?

This is the setup. I have a class:

public class MyClass
{
    public int MyValue { get; set; }
    public int MyOtherValue { get; set; }
}

And two lists:

List<MyClass> list1 = new List<MyClass>();
list1.Add(new MyClass() { MyValue = 1, MyOtherValue = 10 });
list1.Add(new MyClass() { MyValue = 2, MyOtherValue = 20 });
list1.Add(new MyClass() { MyValue = 3, MyOtherValue = 30 });
list1.Add(new MyClass() { MyValue = 4, MyOtherValue = 40 });

List<MyClass> list2 = new List<MyClass>();
list2.Add(new MyClass() { MyValue = 2, MyOtherValue = 50 });
list2.Add(new MyClass() { MyValue = 3, MyOtherValue = 60 });

I want to remove all list2 instances from list1 matching the MyValue property.

Solution

Method 1: Loop and LINQ

list2.ForEach(l2 => { list1.RemoveAll(l1 => l1.MyValue == l2.MyValue); });


Method 2: LINQ and Loop

(from l1 in list1 join l2 in list2 on l1.MyValue equals l2.MyValue
 select l1).ToList().ForEach(i => list1.Remove(i));

Output (from both)

MyValue: 1 MyOtherValue: 10
MyValue: 4 MyOtherValue: 40

Performance

Don't be fooled that Method1 may look prettier than Method2. Performance depends on the size of the lists. Method1 scales very badly when list2 has more than a few elements compared to the size of list1.

List1 size: 1.000.000
{
     List2 size: 10
     Method 1: 103 ms
     Method 2: 325 ms

     List2 size: 100
     Method 1: 5534 ms
     Method 2: 682 ms
}

List1 size: 1.000
{
     List2 size: 100
     Method 1: 1 ms
     Method 2: 11 ms

     List2 size: 900
     Method 1: 32 ms
     Method 2: 12 ms
}

And it gets all bad when both lists are large:

List1 size: 100.000
{
     List2 size: 90.000
     Method 1: 297834 ms
     Method 2: 7555 ms
}

Conclusion

Use Method2 unless you are sure that the list that you want to remove is very small compared to the main list (< 50 elements).

History

  • 2009-11-06: First version

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Henrik Sterndorff Jessen

Software Developer

Denmark Denmark

Member


Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMy vote of 2 PinmemberPaulo Zemek1:02 10 Nov '09  
GeneralRe: My vote of 2 PinmemberHenrik Sterndorff Jessen6:27 11 Nov '09  
That is very true - and performance is way better. But does the dictionary/keyed list approach not require that you know in advance what property you want to search by? Of cause you can always build two new dictionaries and filter using them. But I think that makes less readable code (unless you hide it away in an utility method).
 
Anyways... I was looking for an easy one-liner. Do you have a better way to do it (sans the dictionary approach)?

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120210.1 | Last Updated 9 Nov 2009
Article Copyright 2009 by Henrik Sterndorff Jessen
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid