List<object>.RemoveAll - How to create an appropriate Predicate

Posted by CJM on Stack Overflow See other posts from Stack Overflow or by CJM
Published on 2010-06-18T12:12:12Z Indexed on 2010/06/18 12:33 UTC
Read the original article Hit count: 319

This is a bit of noob question - I'm still fairly new to C# and generics and completely new to predicates, delegates and lamda expressions...

I have a class 'Enquiries' which contains a generic list of another class called 'Vehicles'. I'm building up the code to add/edit/delete Vehicles from the parent Enquiry. And at the moment, I'm specifically looking at deletions.

From what I've read so far, it appears that I can use Vehicles.RemoveAll() to delete an item with a particular VehicleID or all items with a particular EnquiryID. My problem is understanding how to feed .RemoveAll the right predicate - the examples I have seen are too simplistic (or perhaps I am too simplistic given my lack of knowledge of predicates, delegates and lambda expressions).

So if I had a List<Of Vehicle> Vehicles where each Vehicle had an EnquiryID, how would I use Vehicles.RemoveAll() to remove all vehicles for a given EnquiryID?

I understand there are several approaches to this so I'd be keen to hear the differences between approaches - as much as I need to get something working, this is also a learning exercise.

As an supplementary question, is a Generic list the best repository for these objects? My first inclination was towards a Collection, but it appears I am out of date. Certainly Generics seem to be preferred, but I'm curious as to other alternatives.

Thanks

© Stack Overflow or respective owner

Related posts about c#

Related posts about generics