Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

LINQ and WMI results - why not?

DZone's Guide to

LINQ and WMI results - why not?

·
Free Resource

WMI (Windows Management Instrumentation) can be queried by using standard ManagementObjectSearcher and then going through the items in the collection via a foreach loop.

Seems simple enough. However, WMI results can also be queried via LINQ. For example, I instantiate a ManagementObjectSearcher that will get the data that is specific to Win32_Processor:

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()
select x;

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()
select x;

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"""
select c;

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.

Topics:

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}