LINQ and WMI results - why not?
LINQ and WMI results - why not?
Join the DZone community and get the full member experience.Join For Free
WMI (Windows Management Instrumentation) can be queried by using standard ManagementObjectSearcher and then going through the items in the collection via a foreach loop.
ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_Processor");
How would you generally go when you need to find an item in the collection above? Probably use a foreach loop, right?
In this case, you will probably use something very similar to this:
foreach (ManagementObject obj in searcher.Get())
// Action here
It works fine as long as you want to go through each item in the collection. However, what would you do if you needed to select only the items that fit a specific criteria that are already placed in the collection (this does not involve initial WMI query modification)?
Using an if statement is fine as long as you are querying against a single criteria pointer. But what if there are multiple parameters by which you want to select the needed instance of ManagementObject?
In case you want to make the object search more specific, you can change the asterisk to a specific property in the initial query. For the existing collection, you can use LINQ to simplify the process. The first possible LINQ query that comes to mind is probably this.
var unit = from x in searcher.Get()
In fact, this is valid LINQ syntax and it looks like it should work just fine. However, it does not apply to ManagementObjectSearcher. ManagementObjectCollection (that is returned by the Get method) does not implement a query pattern, therefore you cannot directly invoke it via a simple LINQ query.
The solution to this problem is quite simple – you need to explicitly declare the type of x that will be selected from the collection – it is a ManagementObject. To do this, modify the query to look like this:
var unit = from ManagementObject x in searcher.Get()
This will automatically generate an IEnumerable<ManagementObject> compatible collection that fits the criteria set in the searcher. Compared to a foreach loop, this method is more efficient because you can directly specify multiple criteria pointers. For example:
var unit = from ManagementObject c in searcher.Get()
where c.Properties["CpuStatus"].Value.ToString() == "1" && c.Path.ToString() == @"\\DENNIS-PC\root\cimv2:Win32_Processor.DeviceID=""CPU0"""
A similar query (although with a single criteria pointer) can be simplified this way:
var unit = searcher.Get().Cast<ManagementObject>().ToList().Where((c) => c.Properties["CpuStatus"].Value.ToString() == "1");
So instead of directly using where and select, I am building a lambda expression that pretty much does the same thing, only in a shorter manner. Also, I am directly casting the ManagementObjectCollection to a list, so it can be queried without explicit object declaration for x.
I generally prefer using LINQ instead of looping through the results for one reason – LINQ statements are very flexible and I can easily modify them to conform to new requirements or needs. WMI is just one of the areas where LINQ can make life a bit easier.
Opinions expressed by DZone contributors are their own.