Removing elements from binding list
You could try:
UserList = UserList.Where(x => x.id == ID).ToList();
If you use RemoveAll()
inside a generic class that you intend to be used to hold a collection of any type object, like this:
public class SomeClass<T>
{
internal List<T> InternalList;
public SomeClass() { InternalList = new List<T>(); }
public void RemoveAll(T theValue)
{
// this will work
InternalList.RemoveAll(x =< x.Equals(theValue));
// the usual form of Lambda Predicate
//for RemoveAll will not compile
// error: Cannot apply operator '==' to operands of Type 'T' and 'T'
// InternalList.RemoveAll(x =&gt; x == theValue);
}
}
This content is taken from here.
It's not working because you are working on a copy of the list which you created by calling ToList()
.
BindingList<T>
doesn't support RemoveAll()
: it's a List<T>
feature only, so:
IReadOnlyList<User> usersToRemove = UserList.Where(x => (x.id == ID)).
ToList();
foreach (User user in usersToRemove)
{
UserList.Remove(user);
}
We're calling ToList()
here because otherwise we'll enumerate a collection while modifying it.