Convert Linq to Regular Function

I have this linq method, how to get all the properties of a computer network card, and I do not want to use linq, can I help convert it and not use Linq?

public NetworkAdapter[] GetAll() { return (from adapter in NetworkInterface.GetAllNetworkInterfaces() from uniCast in adapter.GetIPProperties().UnicastAddresses where !System.Net.IPAddress.IsLoopback(uniCast.Address) && uniCast.Address.AddressFamily != AddressFamily.InterNetworkV6 let lastGatewayAddress = adapter.GetIPProperties().GatewayAddresses.LastOrDefault() select new NetworkAdapter() { string Name = adapter.Name, string ID = adapter.Id, string Description = adapter.Description, string IPAddress = uniCast.Address.ToString(), string NetworkInterfaceType = adapter.NetworkInterfaceType.ToString(), string Speed = adapter.Speed.ToString("#,##0"), string MacAddress = getMacAddress(adapter.GetPhysicalAddress().ToString()), string gatewayIpAddress = string.Join(" ", adapter.GetIPProperties().GatewayAddresses.Select(a => a.Address)) }).ToArray(); } 

this is what i tried;

 public void get() { NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces(); foreach (NetworkInterface adapter in nics) { Description = adapter.Description; Name = adapter.Name; MacAddress = adapter.GetPhysicalAddress().ToString(); IPInterfaceProperties ips = adapter.GetIPProperties(); UnicastIPAddressInformationCollection uniCast = ips.UnicastAddresses; foreach (UnicastIPAddressInformation ipInfo in uniCast) { if (ipInfo.Address.AddressFamily != AddressFamily.InterNetworkV6) { } } } } 
+4
source share
2 answers

Although I do not understand why, here it goes.

With a little help from my friend ReSharper (and my input, because ReSharper was not able to do all this):

 public NetworkAdapter[] GetAll() { List<NetworkAdapter> list = new List<NetworkAdapter>(); foreach (NetworkInterface adapter in NetworkInterface.GetAllNetworkInterfaces()) foreach (UnicastIPAddressInformation uniCast in adapter.GetIPProperties().UnicastAddresses) { if (!System.Net.IPAddress.IsLoopback(uniCast.Address) && uniCast.Address.AddressFamily != AddressFamily.InterNetworkV6) { StringBuilder gatewayIPAddresses = new StringBuilder(); string gatewayIPAddressesDisplay = string.Empty; foreach (var address in adapter.GetIPProperties().GatewayAddresses) { gatewayIPAddresses.Append(address.Address); gatewayIPAddresses.Append(" "); } if (gatewayIPAddresses.Length > 0) { gatewayIPAddressesDisplay = gatewayIPAddresses.ToString().TrimEnd(' '); } list.Add(new NetworkAdapter() { Name = adapter.Name, ID = adapter.Id, Description = adapter.Description, IPAddress = uniCast.Address.ToString(), NetworkInterfaceType = adapter.NetworkInterfaceType.ToString(), Speed = adapter.Speed.ToString("#,##0"), MacAddress = getMacAddress(adapter.GetPhysicalAddress().ToString()), gatewayIpAddress = gatewayIPAddressesDisplay }); } } return list.ToArray(); } 
+5
source

Try the following:

 public NetworkAdapter[] GetAll() { var networkAdapters = new List<NetworkAdapter>(); foreach (NetworkInterface adapter in NetworkInterface.GetAllNetworkInterfaces()) { foreach (UnicastIPAddressInformation uniCast in adapter.GetIPProperties().UnicastAddresses) { if (!System.Net.IPAddress.IsLoopback(uniCast.Address) && uniCast.Address.AddressFamily != AddressFamily.InterNetworkV6) { GatewayIPAddressInformation lastGatewayAddress = adapter.GetIPProperties().GatewayAddresses.LastOrDefault(); networkAdapters.Add(new NetworkAdapter() { Name = adapter.Name, ID = adapter.Id, Description = adapter.Description, IPAddress = uniCast.Address.ToString(), NetworkInterfaceType = adapter.NetworkInterfaceType.ToString(), Speed = adapter.Speed.ToString("#,##0"), MacAddress = getMacAddress(adapter.GetPhysicalAddress().ToString()), gatewayIpAddress = string.Join(" ", adapter.GetIPProperties().GatewayAddresses.Select(a => a.Address)) }); } } } return networkAdapters.ToArray(); } 
0
source

All Articles