I'm working with some open-source C# code for a USB HID library that I'm trying to modify. My C# (and OOP in general) are VERY rusty, so forgive the basic question.
The premise is simple: check for enumerated HID devices, and if one of the devices matches my desired VID and PID, get and return the full path to the device. The first part (checking for a match) work fine, but I can't seem to figure out the syntax to then return the path to that same matching device.
public static string IsConnected(string deviceVID, string devicePID)
if (EnumerateDevices().Any(x => x.VID == deviceVID && x.PID == devicePID))
return EnumerateDevices().Select(x => x.Path).Where(x => x.Attributes.ProductId == devicePID);
That's because you want first the paths (a string collection), with the
Select(), and then filtering your collection, with the
Where(), where you don't have any
Your LINQ query should apply first your filter and then select the path of the object you're looking for:
return EnumerateDevices().Where(x => x.Attributes.ProductId == devicePID).Select(x => x.Path).SingleOrDefault();