2016-03-31 74 views
2

有兩個集合: NodesWithCircuitsDown<NetworkDeviceNodeStatus>RecordedImpairedNodes<NetworkDeviceNodeStatus>LINQ比較兩個集合的多個屬性

NetworkDeviceNodeStatus有一個NodeId(int)和一個CurrentStatus(枚舉)。

我想創建第三個集合稱爲NodesWithDifferentImpairment,將包含有NodeId這就是在上述兩個集合的任何NetworkDeviceNodeStatus,但有CurrentStatus這是不同的。

下面是我到目前爲止,但我有困難嵌套查詢來完成此操作。

IEnumerable<NetworkDeviceNodeStatus> NodesWithDifferentImpairment = 
       NodesWithCircuitsDown.Where(x => 
        RecordedImpairedNodes.Select(y => new { y.CurrentStatus, y.NodeId }).Select(y => y.NodeId) 
      ); 

回答

3

試試這個

NodesWithCircuitsDown.Join(RecordedImpairedNodes, 
    node => node.NodeId, 
    node => node.NodeId, 
    (leftNode, rightNode) => new { LeftNode = leftNode, RightNode = rightNode }). 
    Where(pair => pair.LeftNode.CurrentStatus != pair.RightNode.CurrentStatus); 

要通過節點Id屬性加盟兩套,提取對和篩選那些具有不同的地位得到了對不同狀態的節點。

+0

您的意思是'對=> pair.LeftNode.CurrentStatus = pair.RightNode.CurrentStatus'!? – blgrnboy

+0

@blgrnboy是的,感謝您發現這個 –

+0

我還添加了'.Select(pair => pair.LeftNode)'以獲取一個'IEnumerable '?最終結果應該是找回'NodesWithCircuitsDown'中沒有相同CurrentStatus的項目。 – blgrnboy

2

你必須加入他們,然後過濾:

var NodesWithDifferentImpairment = from nwcd in NodesWithCircuitsDown 
            join rin in RecordedImpairedNodes on nwcd.NodId equals rin.NodeId 
            where rin.CurrentStatus != nwcd.CurrentStatus 
            select new NetworkDeviceNodeStatus 
              { 
              CurrentStatus = rin.CurrentStatus, 
              NodeId = rin.NodeId 

              };