用Linq优化WMI查询



首先,我应该承认我真的不太了解如何使用WMI或LINQ。所以我相信任何人都不会惊讶我有这么困难的时候!

问题是,我试图从同一个WMI对象查询或返回几个值。通常情况下,这不会太糟糕,但由于只需要返回与特定数据集匹配的元素,我发现很难对此进行思考。问题在于,我必须首先在WMI中查询特定元素或值,然后重新启动查询,按所述元素或值进行过滤,以返回所需的数据集。

我确信我可以循环遍历WMI对象中的所有元素并将它们附加到集合中。然后循环遍历集合,直到我得到我要找的东西,但我仍然有过滤掉所有多余的不必要元素的问题。

所以我想我的问题是,做WMI对象的高级搜索的最好方法是什么,我如何优化这个过程,这样就不会花那么长时间来检索我正在寻找的信息?

在我的许多搜索中,我发现了一些关于使用LINQ的人的参考资料,但我对LINQ的有限接触意味着我真的不确定如何在这种情况下使用它,所以任何帮助或见解将非常感谢。

这是我目前想到的:

        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;

所以我不熟悉您的特定DB提供程序,但您的主要问题是您迭代IEnumerable 10次,这意味着对数据库的10个不同查询。你当然不需要这样做,而且这会影响你的表现。您还对返回的数据进行了10次不同的排序;你应该只做一次。

这里是你的代码的重构,获取项目一次,然后得到10个不同的值:

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;

请注意,如果可能的话,您应该在DB端进行排序,并且还将DB端的输出限制为仅1个值。在不知道您正在使用的查询提供程序的细节的情况下,我不能说这样做的最佳方式是什么。

创建一个WMI查询来获取您需要的数据,将其转换为LINQ to Objects查询,然后遍历结果,获取您想要的数据位会更容易。

var queryStr =
    "SELECT * " +
    "FROM Win32_NetworkAdapterConfiguration " +
    "WHERE IPEnabled = true";
using (var searcher = new ManagementObjectSearcher(queryStr))
using (var managementQuery = searcher.Get())
{
    // convert to LINQ to Objects query
    var query =
        from ManagementObject mo in managementQuery
        orderby Convert.ToUInt32(mo["IPConnectionMetric"])
        select new
        {
            Description          = Convert.ToString(mo["Description"]),
            Index                = Convert.ToString(mo["Index"]),
            MACAddress           = Convert.ToString(mo["MACAddress"]),
            IPAddress            = String.Join(", ", (string[])mo["IPAddress"] ?? new string[0]),
            IPSubnet             = String.Join(", ", (string[])mo["IPSubnet"] ?? new string[0]),
            DefaultIPGateway     = String.Join(", ", (string[])mo["DefaultIPGateway"] ?? new string[0]),
            DNSServerSearchOrder = String.Join(", ", (string[])mo["DNSServerSearchOrder"] ?? new string[0]),
        };
    // grab the fields
    String description = null;
    foreach (var item in query)
    {
        description  = description  ?? item.Description;
        _NICINDEX    = _NICINDEX    ?? item.Index;
        _MACADDRESS  = _MACADDRESS  ?? item.MACAddress;
        _IPADDRESS   = _IPADDRESS   ?? item.IPAddress;
        _IPV6ADDRESS = _IPV6ADDRESS ?? item.IPAddress;
        _SUBNETMASK  = _SUBNETMASK  ?? item.IPSubnet;
        _GATEWAY     = _GATEWAY     ?? item.DefaultIPGateway;
        _DNSSERVER   = _DNSSERVER   ?? item.DNSServerSearchOrder;
        _DNSSECSVR   = _DNSSECSVR   ?? item.DNSServerSearchOrder;
        // check if all fields are set so we can stop
        if (new[] { description, _NICINDEX, _MACADDRESS, _IPADDRESS, _IPV6ADDRESS, _SUBNETMASK, _GATEWAY, _DNSSERVER, _DNSSECSVR }.All(x => x != null))
            break;
    }
}

虽然我不清楚为什么你想要单独获得每个字段的第一个。我希望您将第一个适配器的字段作为一个整体。所以最后,我认为最后应该是:

var first = query.FirstOrDefault();
if (first != null)
{
    description  = first.Description;
    _NICINDEX    = first.Index;
    _MACADDRESS  = first.MACAddress;
    _IPADDRESS   = first.IPAddress;
    _IPV6ADDRESS = first.IPAddress;
    _SUBNETMASK  = first.IPSubnet;
    _GATEWAY     = first.DefaultIPGateway;
    _DNSSERVER   = first.DNSServerSearchOrder;
    _DNSSECSVR   = first.DNSServerSearchOrder;
}

相关内容

  • 没有找到相关文章

最新更新