我只想使用LINQ语句以下方法。我返回字符串列表,但首先我得到了NetworkInterface列表。使用LINQ,C#和列表重构网络接口
public static List<string> ObtenerDireccionesDeInterfacesDeRedActivos()
{
var listaDirecciones = new List<string>();
var interfacesActivos =
(from networkInterface in NetworkInterface.GetAllNetworkInterfaces()
let
/*IPv4InterfaceStatistics*/
statistics = networkInterface.GetIPv4Statistics()
where
// filter so we see only Internet adapters
networkInterface.OperationalStatus == OperationalStatus.Up
&& networkInterface.NetworkInterfaceType != NetworkInterfaceType.Tunnel
&& networkInterface.NetworkInterfaceType != NetworkInterfaceType.Loopback
// all testing seems to prove that once an interface comes online
// it has already accrued statistics for both received and sent...
&& (statistics.BytesReceived > 0) && (statistics.BytesSent > 0)
select
networkInterface).ToList<NetworkInterface>();
foreach (NetworkInterface nic in interfacesActivos)
{
var ips = nic.GetIPProperties().UnicastAddresses;
foreach (var ip in ips)
{
listaDirecciones.Add(ip.Address.ToString());
}
}
return listaDirecciones;
}
有什么建议吗?
就个人而言,我更喜欢@ cdhowie的回答我的,但如果你想返回一个`名单 `,我的解决方案应该工作。 –
Jacob
2011-01-31 19:41:55