我有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;
}
}
}
Negative Voter ..小心解釋一下嗎? – Marshal