Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9085671
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T21:18:03+00:00 2026-06-16T21:18:03+00:00

First, I should start out by admitting that I really don’t know a lot

  • 0

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;
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T21:18:04+00:00Added an answer on June 16, 2026 at 9:18 pm

    So I’m not familiar with your particular DB provider, but your primary problem is that you’re iterating the IEnumerable 10 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:

    ManagementObjectSearcher searcher = new ManagementObjectSearcher
                ("SELECT * FROM Win32_NetworkAdapterConfiguration where IPEnabled=true");
    ManagementObject managementObject = searcher.Get()
        .Cast<ManagementObject>()
        .OrderBy(obj => obj["IPConnectionMetric"])
        .FirstOrDefault();
    
    string description = managementObject["Description"];
    _NICINDEX  = managementObject["Index"];
    //repeat for the other 8 values following the above pattern
    return description;
    

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

First off I should say that I don't have any experience in working with
First, I'd like to start out expressing that I am not trying to just
The above expression is working fine. this expression means first leter should start with
My application should load some data when it start at first time, but it
First you should know I have looked into many questions and none of them
Why is it that both outs are printing 84? The first one should read
I am trying to layout a bunch of overlapping rectangles that start out like
I'm about to start building a Rails app that will eventually need to vary
session_start() should be the first line of web page. Dreamweaver's php templates should be
Maybe I should first give an idea of what I want to accomplish as

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.