使用LINQ检索具有IP地址的MAC地址

时间:2012-01-09 13:08:59

标签: c# linq

我想检索带有IP地址的MAC地址......

var nics = NetworkInterface.GetAllNetworkInterfaces()
    .Where(ipProp => ipProp.GetIPProperties().UnicastAddresses
                     .Where(ip => ip.Address.ToString().Equals("192.168.1.111"))
    );

我收到错误:

  

“无法将类型'System.Collections.Generic.IEnumerable'隐式转换为'bool'”

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:3)

NetworkInterface networkInterface = NetworkInterface.GetAllNetworkInterfaces().Where(ipProp => ipProp.GetIPProperties().UnicastAddresses.FirstOrDefault(ip => ip.Address.ToString().Equals("YOUR_IP")) != null).FirstOrDefault();
if (networkInterface != null)
{
    Console.WriteLine(networkInterface.GetPhysicalAddress());
}