I recently needed to do some WMI queries so I went hunting for a LINQ implementation. I found a great basic implementation that Emile Bosch did way back in 2005. Once you have finished reading this post you should view his blog post for more detail on the implementation.
Unfortunately Emile's WMILINQ implementation no longer works as it was based on a beta version of the framework so I brushed it off and upgraded it to work with the RTM release of .Net 3.5. I also made several other other improvements such as:
Doing basic queries with the provider is really easy, but be aware that the implementation is very limited. It does not support projections, nested queries and referenced variables. So for example the following query generates an incorrect WQL statement.
One would expect it to generate this statement:
but instead executes this:
Regardless of these shortcomings I still found it quite useful, and I wanted to share the upgraded version with you. So if you'd like to grab it, here's the download.
I am currently working on a rewrite of the implementation using Matt Warren's excellent set of Building an IQueryable Provider blog posts as a basis. So far it is not looking too bad and it overcomes several of the problems with this implementation, but it requires quite a bit more effort to finish it off, so it may be a while before I release it.
Image may be NSFW.
Clik here to view.
Unfortunately Emile's WMILINQ implementation no longer works as it was based on a beta version of the framework so I brushed it off and upgraded it to work with the RTM release of .Net 3.5. I also made several other other improvements such as:
- Updated the code generator to identify and generate array properties and emit a generated code attribute on the associated class
- Fixed a bug with string where filter parameters
- Improved the CIM Type mapping conversion logic
- Added array mapping support to the LINQ query
- Changed the name to match the new Microsoft LINQ to X naming convention
var query = from account in context.Source<Win32_UserAccount>()
select account;
foreach (Win32_UserAccount account in query)
{
Console.WriteLine(account.Name);
}
select account;
foreach (Win32_UserAccount account in query)
{
Console.WriteLine(account.Name);
}
Doing basic queries with the provider is really easy, but be aware that the implementation is very limited. It does not support projections, nested queries and referenced variables. So for example the following query generates an incorrect WQL statement.
string name = "Me";
var query = from account in context.Source<Win32_UserAccount>()
where account.Name == name
select account;
var query = from account in context.Source<Win32_UserAccount>()
where account.Name == name
select account;
One would expect it to generate this statement:
SELECT * FROM Win32_UserAccount WHERE name = 'Me'
but instead executes this:
SELECT * FROM Win32_UserAccount WHERE name = 'name'
Regardless of these shortcomings I still found it quite useful, and I wanted to share the upgraded version with you. So if you'd like to grab it, here's the download.
I am currently working on a rewrite of the implementation using Matt Warren's excellent set of Building an IQueryable Provider blog posts as a basis. So far it is not looking too bad and it overcomes several of the problems with this implementation, but it requires quite a bit more effort to finish it off, so it may be a while before I release it.
Image may be NSFW.
Clik here to view.
