NewLife/X

去除重复IP地址
智能大石头 authored at 2024-12-03 00:25:10
4d2fc34
Tree
1 Parent(s) c580ebe
Summary: 1 changed files with 16 additions and 3 deletions.
Modified +16 -3
Modified +16 -3
diff --git a/NewLife.Core/Net/NetHelper.cs b/NewLife.Core/Net/NetHelper.cs
index d95fe05..10acd06 100644
--- a/NewLife.Core/Net/NetHelper.cs
+++ b/NewLife.Core/Net/NetHelper.cs
@@ -424,9 +424,22 @@ public static class NetHelper
 
         // 带网关的接口地址很重要,优先返回
         // Linux下不支持PrefixOrigin
-        var ips = dic.OrderByDescending(e => e.Value)
-            //.ThenByDescending(e => e.Key.PrefixOrigin == PrefixOrigin.Dhcp || e.Key.PrefixOrigin == PrefixOrigin.Manual)
-            .Select(e => e.Key.Address).ToList();
+        //var ips = dic.OrderByDescending(e => e.Value)
+        //    //.ThenByDescending(e => e.Key.PrefixOrigin == PrefixOrigin.Dhcp || e.Key.PrefixOrigin == PrefixOrigin.Manual)
+        //    .Select(e => e.Key.Address).ToList();
+
+        // 去除重复IP地址
+        var ips = new List<IPAddress>();
+        var hash = new List<String>();
+        foreach (var item in dic.OrderByDescending(e => e.Value))
+        {
+            var address = item.Key.Address + "";
+            if (!hash.Contains(address))
+            {
+                ips.Add(item.Key.Address);
+                hash.Add(address);
+            }
+        }
 
         return ips;
     }