What is the best way to filter the contents of an arraylist?
Solution 1
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.
ArrayList subSetList = new ArrayList(CompleteList.Cast<USBDevice>()
.Where(d => d.VID == "10C4")
.ToList());
But this seems rather pointless. Why are you using the old ArrayList
class and LINQ in the same project? You should try to start using the List<T>
class in the System.Collections.Generic
namespace instead, then your where expression will work without any casting, just as you want.
Solution 2
To go along with @Mark Byers' suggestion of using a List<T>, here is some example code:
List<USBDevice> CompleteList = new List<USBDevice>();
CompleteList.Add(new USBDevice(){VID = "10C4", Other = "x"});
CompleteList.Add(new USBDevice() { VID = "10C4", Other = "x" });
//..Fill CompleteList with all attached devices....
List<USBDevice> SubSetList = new List<USBDevice>();
SubSetList = CompleteList.Where(d => d.VID.Equals("10C4")).ToList();
Solution 3
Yes, you can use Where. But in order to do that you need to cast your ArrayList to USBDevice. This should work:
var subset = CompleteList.Cast<USBDevice>().Where(x =>d.VID = "10C4");
But if you're able to use Linq, why are u using ArrayLists? You should be using generic collections like List instead.
Edit: As Nick pointed out in the comment, using OfType() is recommended if your ArrayList may contain anything other than USBDevice objects.
Comments
-
PICyourBrain about 2 years
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")
-
Nick Gotch over 14 yearsIf he's using an ArrayList other types may be in the list. Using .OfType<USBDevice>() instead of .Cast<USBDevice>() would avoid exceptions for invalid types.
-
Mark Byers over 14 years@Nick: Exceptions are good - they let you know that you made an error rather than just carrying on like nothing is wrong.
-
Nick Gotch over 14 years@Mark: True but it depends on why he's using an ArrayList. If it's to hold all kinds of "Devices" that don't inherit from a common type this would safely filter out what he's not looking for.