0

我有一个对象列表。我的对象是NetworkAdapter代表我机器上的网卡。此网卡具有属性 ID,例如:{1106B232-363f-417F-9DC9-643BB02BEDE2} 当然还有 Ip 地址、名称等。我的问题是,如果我的网卡有超过 1 个 Ip 地址,那么我的应用程序会显示同一个网卡两次(每次使用不同的ip) 但是因为这是同一张网络卡所以 ID 是相同的所以我想要做的是拿这个List<NetworkAdapter>并删除具有相同 ID 的重复网卡。

这是我在机器上获取所有适配器的功能:

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();
}

最好的方法是什么?

4

3 回答 3

2

类似于下面的东西来获得不同的项目

var newList = YourClass.GetAll()
              .GroupBy(n=>n.ID).Select(g=>g.FirstOrDefault())
              .ToList();

或在添加前检查

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
    });

}
于 2013-05-17T12:28:53.127 回答
0
list = list.GroupBy(adapter => adapter.Id,(key, group) => group.First()).toList();

应该删除所有具有相同 ID 的重复对象。

于 2013-05-17T12:33:38.457 回答
0

一种可能对多个领域有用的方法是 DistinctBy 方法,该方法将根据您赋予它们的属性在列表中产生不同的元素:

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;
        }
    }
}

用法:

var newList = oldList.DistinctBy(adapter => adapter.Id).ToList();
于 2013-05-17T12:39:52.937 回答