Say I have an ArrayList of USBDevice Objects. Each USBDevice has ProductID and VendorID properties (among others). I want to create another ArrayList that is a subset of the first that contains only USBDevice that match a specific VID. What is the shortest way of doing this? I haven’t tried this yet but can lambda expressions be used like this…
ArrayList CompleteList = new ArrayList();
...
// Fill CompleteList with all attached devices....
...
ArrayList SubSetList = CompleteList.Where(d => d.VID == "10C4")
You need a cast. The only thing the compiler knows about ArrayLists is that they contain objects. It doesn’t know the types of the objects inside, so you have to tell it.
But this seems rather pointless. Why are you using the old
ArrayListclass and LINQ in the same project? You should try to start using theList<T>class in theSystem.Collections.Genericnamespace instead, then your where expression will work without any casting, just as you want.