2016-12-28 54 views
2

我有2個設備類,尋找LINQ表達式foreach循環

public class Device1 
{ 
    public string DeviceName { get; set; } 
    public string IP { get; set; } 
    public bool IsExist { get; set; } 
} 
public class Device2 
{ 
    public string DeviceName { get; set; } 
    public string DeviceIP { get; set; } 
} 

爲 「ISEXIST」 的當前值是 「Device1的[]」 陣列 「假」 時,

private static Device1[] GetDevice1Arr() 
    { 
     List<Device1> d1List = new List<Device1>() { 
      new Device1 { DeviceName="d1", IP="1", IsExist=false}, 
      new Device1 { DeviceName="d2", IP="1", IsExist=false} 
     }; 

     return d1List.ToArray(); 
    } 

現在「設備2 []」 陣列不具有 「ISEXIST」,

private static Device2[] GetDevice2Arr() 
    { 
     List<Device2> d2List = new List<Device2>() { 
      new Device2 { DeviceName="d1", DeviceIP="3"}, 
      new Device2 { DeviceName="d2", DeviceIP="1"}, 
      new Device2 { DeviceName="d2", DeviceIP="2"}, 
      new Device2 { DeviceName="d3", DeviceIP="3"} 
     }; 

     return d2List.ToArray(); 
    } 

現在我比較兩個數組 「Device1的[]」 和 「設備2 []」 通過使用2 「foreach」 循環,如果器件N ame和DeviceIP是一樣的,我正在重置「IsExist」=「true」。

尋找LINQ這裏更換或任何替代方法。謝謝!

Device1[] d1 = GetDevice1Arr(); 
Device2[] d2 = GetDevice2Arr(); 

foreach(var device1 in d1) 
{ 
    foreach(var device2 in d2) 
    { 
     if(device2.DeviceName == device1.DeviceName && device2.DeviceIP == device1.IP) 
     { 
      device1.IsExist = true; 
     } 
    } 
} 

回答

3

,可隨時更換foreach循環使用LINQ,但不是之一,因爲LINQ的是查詢更新。你有什麼本質上是一個Any查詢(不任何d2匹配這個條件?):

Device1[] d1 = GetDevice1Arr(); 
Device2[] d2 = GetDevice2Arr(); 

foreach(var device1 in d1) 
{ 
    device1.IsExist = d2.Any(device2 => 
          device2.DeviceName == device1.DeviceName 
          && device2.DeviceIP == device1.IP)); 
} 

有可能是使用IntersectJoinWhere等來查找需要的項目,以替代方法更新,但最後一個foreach循環是更新它們的正確方法。

+0

Negative Voter ..小心解釋一下嗎? – Marshal

3

看起來你正在嘗試加入。你可以這樣做,在LINQ,但你仍然需要一個foreach對結果更新IsExist

var itemsToUpdate = from d1 in GetDevice1Arr() 
        join d2 in GetDevice2Arr() 
         on new { d1.DeviceName, d1.IP } 
         equals new { d2.DeviceName, IP = d2.DeviceIP } 
        select d1; 

foreach(var d1 in itemsToUpdate) 
    d1.IsExist = true; 
+0

非常好。這裏唯一的版本使得我的混亂比原來更易讀。我通常更喜歡點語法。但是這真的顯示出類似於SQL的力量。 :-) –

0

既然都是列表(類型強制轉換爲數組),你可以使用List.ForEach遍歷第一個列表,和Any迭代內部列表。

d1.ForEach(d=> d.IsExist = d2.Any(x => x.DeviceIP == d.IP && x.DeviceName == d.DeviceName); 

這一個,而所有其他解決方案使用兩個級別的迭代,並且只是速記到您現有的解決方案。你無法擺脫它。

+0

非常感謝短信 –

0

與使用加入,但作爲一個模擬的 '左' 加入以獲得真或假的另一項建議:

Device1[] d1 = GetDevice1Arr(); 
Device2[] d2 = GetDevice2Arr(); 

foreach(var d in from dev1 in d1 
    join dd in d2 on new {dev1.DeviceName, dev1.IP} equals new {dd.DeviceName, IP = dd.DeviceIP} into d3  
    select new {dev1, Exists = d3.Any()}) 
    d.dev1.IsExist= d.Exists; 
3

一個襯墊

d1.Where(dev1=> d2.Any(dev2=>dev2.DeviceName == dev1.DeviceName && 
          dev2.DeviceIP == dev1.IP)) 
    .ToList() 
    .ForEach(dev1=>dev1.IsExist = true); 

最終輸出

d1.Dump(); //LinqPad feature 

enter image description here

+0

令人驚歎,非常感謝 –

0
d1.Where(x => d2.Any(y => x.IsExist = (x.DeviceName == y.DeviceName && x.IP == y.DeviceIP))).ToList();