Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/263.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# LINQ比较多个属性上的两个集合_C#_Linq - Fatal编程技术网

C# LINQ比较多个属性上的两个集合

C# LINQ比较多个属性上的两个集合,c#,linq,C#,Linq,有两个集合: 带有电路向下的节点和记录的MPairedNodes networkdevicedestatus有一个NodeId(int)和一个CurrentStatus(enum) 我想创建一个名为NodesWithDifferentImpairment的第三个集合,该集合将包含任何NetworkDeviceNodeStatus,其中NodeId位于上述两个集合中,但CurrentStatus则不同 下面是我到目前为止所做的,但是我在嵌套查询以实现这一点时遇到了困难 IEnumerable<

有两个集合:
带有电路向下的节点
记录的MPairedNodes

networkdevicedestatus
有一个
NodeId
(int)和一个
CurrentStatus
(enum)

我想创建一个名为
NodesWithDifferentImpairment
的第三个集合,该集合将包含任何
NetworkDeviceNodeStatus
,其中
NodeId
位于上述两个集合中,但
CurrentStatus
则不同

下面是我到目前为止所做的,但是我在嵌套查询以实现这一点时遇到了困难

IEnumerable<NetworkDeviceNodeStatus> NodesWithDifferentImpairment =
                NodesWithCircuitsDown.Where(x => 
                    RecordedImpairedNodes.Select(y => new { y.CurrentStatus, y.NodeId }).Select(y => y.NodeId)
            );
i具有不同参数的可数节点=
NodesWithCircuitsDown.Where(x=>
RecordedImpairedNodes.Select(y=>new{y.CurrentStatus,y.NodeId})。Select(y=>y.NodeId)
);
试试这个

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

要通过连接NodeId属性上的两个集合来获得具有不同状态的节点对,请提取这些节点对并过滤具有不同状态的节点对。

您必须将它们连接起来,然后过滤:

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

                                           };

你是说
pair=>pair.LeftNode.CurrentStatus!=pair.RightNode.CurrentStatus
?我是否也添加了
。选择(pair=>pair.LeftNode)
以获取
IEnumerable
?最终的结果应该是返回
节点WithCircuitsDown
中不具有相同当前状态的项。另外,请注意,在这种情况下,查询语法(如Ehsan所建议的)可能更具可读性(我只是更习惯于流畅的语法)。