What is the easiest way to foreach through a List removing unwanted objects?

后端 未结 3 1093
北恋
北恋 2021-01-31 23:08

In my application, _collection is a List from which I need to remove all User objects which do not match the criteria.

However, the fol

3条回答
  •  别那么骄傲
    2021-01-31 23:58

    _collection.RemoveAll(user => !user.IsApproved());
    

    If you're still on 2.0:

    _collection.RemoveAll(delegate(User u) { return !u.IsApproved(); });
    

    By the way, if you don't want to touch the original list, you can get another list of approved users with:

    _collection.FindAll(user => user.IsApproved());
    

提交回复
热议问题