c# - Convert Linq to regular function -
i have linq method how machine network card properties , don't want use linq, can have convert , not using linq ?
public networkadapter[] getall() { return (from adapter in networkinterface.getallnetworkinterfaces() unicast in adapter.getipproperties().unicastaddresses !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 have try;
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) { } } } }
although don't understand why, here goes.
with little friend resharper (and input because resharper couldn't all):
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(); }
Comments
Post a Comment