Reputation: 185
I'm trying to use a lambda expression to remove a certain object from a list, based on a value within other list. Here is my code:
List<Client> clientsList = GetSomeList();
SortableSearchableList<Client> clients = new SortableSearchableList<Client>(clientsList);
clients.ToList().RemoveAll(x=> lstPostalCodes.Any(c => c.City.PostalCode == x.PostalCode && c.SubId == x.SubId));
when I use the method RemoveAll indicates that removed certain amount of items but really did not.
Upvotes: 0
Views: 154
Reputation: 185
I solved it this way:
List<Client> clientsList = GetSomeList();
clientsLists.RemoveAll(x=> lstPostalCodes.Any(c => c.City.PostalCode == x.PostalCode && c.SubId == x.SubId));
SortableSearchableList<Client> clients = new SortableSearchableList<Client>(clientsList);
removing the items from the list before creating the SortableSearchableList
Upvotes: 0
Reputation: 203835
You're creating a new list, using the items in clients
to populate it, then you're going through the items in that new list and removing the items that match the condition you have, and then you're dropping that new list on the floor and never using it again. You're not storing it anywhere, or in any way making it acceptable to later code.
If you want the items in clients
to change then you need to remove the items from that collection, rather than creating a new collection and removing the items from that new collection.
Upvotes: 2