DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Trending

  • MQTT, Kafka, NATS, or Zenoh? Choosing Edge-to-Cloud Messaging by Traffic Pattern
  • New York City Taxi Trips: Putting Neo4j and DuckDB to Work
  • When a Coding Agent Fixes the Test Instead of the Bug
  • Member Spotlight: Shai Almong

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

LINQ and WMI results - why not?

By 
Denzel D.
·
Jul. 13, 10 · Interview
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
12.4K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

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.

Windows Management Instrumentation

Opinions expressed by DZone contributors are their own.

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook