Reputation: 4951
i have an List of objects.
my object is NetworkAdapter
that represent the Network Card on my machine.
this Network Card has the property ID for example: {1106B232-363f-417F-9DC9-643BB02BEDE2}
and of course Ip Address, name etc.
my problem is that if i case the Network Card has more than 1 Ip Address so my application shows that same Network Card twice (each time with the different ip) but because this is the same nerwork card so the ID is the same so what i want to do is to take this List<NetworkAdapter>
and remove the duplicate Network Card with the same ID.
this is my function that get all the adapter on the machine:
public static 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(getDevice(adapter.Id))
{
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();
}
what is the best way to do it ?
Upvotes: 0
Views: 215
Reputation: 5324
list = list.GroupBy(adapter => adapter.Id,(key, group) => group.First()).toList();
Should remove all duplicate objects with the same ID.
Upvotes: 0
Reputation: 8511
A method that may be helpful in more than one area is a DistinctBy method that will yield elements in a list that are distinct based on the property you give them:
public static IEnumerable<TElement> DistinctBy<TElement, TProperty>
(this IEnumerable<TElement> elements,
Func<TElement, TProperty> propertySelector)
{
HashSet<TProperty> seenProperties = new HashSet<TProperty>();
foreach (TElement element in elements)
{
if (seenProperties.Add(propertySelector(element)))
{
yield return element;
}
}
}
Usage:
var newList = oldList.DistinctBy(adapter => adapter.Id).ToList();
Upvotes: 0
Reputation: 63065
something similar to below to get the distinct items
var newList = YourClass.GetAll()
.GroupBy(n=>n.ID).Select(g=>g.FirstOrDefault())
.ToList();
Or check before adding
If(!list.Any(l=>l.ID==adapter.Id))
{
list.Add(new NetworkAdapter(getDevice(adapter.Id))
{
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
});
}
Upvotes: 2