First, I should start out by admitting that I really don’t know a lot about using WMI or LINQ for that matter. So I’m sure it wont surprise anyone that I’m having such a difficult time with it!
Trouble is that I’m trying to query or return several values from the same WMI object. Normally this wouldn’t be to bad but I’m finding it difficult to rap my head around this due to a need to only return elements matching a specific set of data. The issue being, that I must first query WMI for a specific element or value and then restart the query, filtering by said element or value to return the required data set.
I’m sure that I could just loop through all the elements in the WMI object and append them to a collection. Then loop through the collection till I get what I’m looking for but then I still have the issue of filtering out all of the extra unnecessary elements.
So I guess my question is, whats the best way to do advanced searches of a WMI object and how can I optimize the process so it doesn’t take so long to retrieve the information I’m looking for?
During my many searches, I found several references to people using LINQ but my limited exposure to LINQ means that I’m not really sure how to use it in this instance so any help or insights would be greatly appreciated.
Here’s what I’ve come up with so far:
ManagementObjectSearcher searcher = new ManagementObjectSearcher
("SELECT * FROM Win32_NetworkAdapterConfiguration where IPEnabled=true");
IEnumerable<ManagementObject> objects = searcher.Get().Cast<ManagementObject>();
string description = (from o in objects orderby o["IPConnectionMetric"]
select o["Description"].ToString()).FirstOrDefault();
_NICINDEX = (from o in objects orderby o["IPConnectionMetric"]
select o["Index"].ToString()).FirstOrDefault();
_MACADDRESS = (from o in objects orderby o["IPConnectionMetric"]
select o["MACAddress"].ToString()).FirstOrDefault();
_IPADDRESS = (from o in objects orderby o["IPConnectionMetric"]
select o["IPAddress"].ToString()).FirstOrDefault();
_IPV6ADDRESS = (from o in objects orderby o["IPConnectionMetric"]
select o["IPAddress"].ToString()).FirstOrDefault();
_SUBNETMASK = (from o in objects orderby o["IPConnectionMetric"]
select o["IPSubnet"].ToString()).FirstOrDefault();
_GATEWAY = (from o in objects orderby o["IPConnectionMetric"]
select o["DefaultIPGateway"].ToString()).FirstOrDefault();
_DNSSERVER = (from o in objects orderby o["IPConnectionMetric"]
select o["DNSServerSearchOrder"].ToString()).FirstOrDefault();
_DNSSECSVR = (from o in objects orderby o["IPConnectionMetric"]
select o["DNSServerSearchOrder"].ToString()).FirstOrDefault();
return description;
So I’m not familiar with your particular DB provider, but your primary problem is that you’re iterating the
IEnumerable10 times, which means 10 different queries to the database. You most certainly don’t need to do that, and it’s really killing your performance. You’re also sorting the returned data 10 different times; you should only do it once.Here’s a refactor of your code to fetch the item once, and then get the 10 different values out of it:
Note that if at all possible you should do the sorting on the DB side, and also restrict the output to just 1 value on the DB side. Without knowing the specifics of the query provider you’re working with, I couldn’t say what the preferable way of doing that is.