I have a class that has an list<Book>
in it, and those Book
objects has many many properties. How can I remove from that list every Book that his level
value is different than, for example, 5?
In this particular case, List<T>.RemoveAll
is probably your friend:
C# 3:
list.RemoveAll(x => x.level != 5);
C# 2:
list.RemoveAll(delegate(Book x) { return x.level != 5; });
list.RemoveAll(bk => bk.Level != 5);
list.RemoveAll(delegate(Book b) { return b.Level == 5; });
Although List.RemoveAll() is an excellent solution, it does a "foreach" on the collection resuling in O(n) or worse performance. If you have lots of items in the list, i would suggest checking out Erick's Index 4 Objects collections.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.