从对象列表中删除重复属性

时间:2013-05-17 12:20:27

标签: c#

我有一个对象列表。 我的对象是NetworkAdapter,代表我机器上的网卡。 此网卡具有属性ID,例如:{1106B232-363f-417F-9DC9-643BB02BEDE2} 当然还有地址,名称等。 我的问题是,如果我的情况下网卡有超过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();
}

最好的方法是什么?

3 个答案:

答案 0 :(得分: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
    });

}

答案 1 :(得分:0)

list = list.GroupBy(adapter => adapter.Id,(key, group) => group.First()).toList();

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

答案 2 :(得分: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();