title: Optimizing WMI query with Linq
body: <p>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!</p>
  
  <p>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. </p>
  
  <p>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. </p>
  
  <p>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? </p>
  
  <p>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.</p>
  
  <p>Here's what I've come up with so far:</p>
  
  <pre><code>        ManagementObjectSearcher searcher = new ManagementObjectSearcher
              ("SELECT * FROM Win32_NetworkAdapterConfiguration where IPEnabled=true");
          IEnumerable&lt;ManagementObject&gt; objects = searcher.Get().Cast&lt;ManagementObject&gt;();
          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;
  </code></pre>