c# - LINQ 比较两个集合的多个属性

标签 c# linq

有两个集合: NodesWithCircuitsDown<NetworkDeviceNodeStatus>RecordedImpairedNodes<NetworkDeviceNodeStatus> .

NetworkDeviceNodeStatus有一个NodeId (int) 和 CurrentStatus (枚举)。

我想创建第三个集合,名为 NodesWithDifferentImpairment ,将包含任何 NetworkDeviceNodeStatusNodeId位于上述两个集合中,但带有 CurrentStatus这是不同的。

下面是我到目前为止所拥有的,但我在嵌套查询来完成此任务时遇到了困难。

IEnumerable<NetworkDeviceNodeStatus> NodesWithDifferentImpairment =
                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 属性上连接两个集合、提取对并过滤具有不同状态的节点来获取具有不同状态的节点对。

关于c# - LINQ 比较两个集合的多个属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36337622/

相关文章:

c# - HTTP 响应中的无效字符集

c# - 添加模型错误 MVC 后如何使验证消息消失?

c# - 如何在 Linq 表达式中处理 IDisposableobject create?

c# - Linq,select().SingleorDefault() 是个坏主意吗?

c# - Linq 外键 |如何只显示具有特定外键的元素?

c# - Linq 查询中的 Select 操作何时变为投影操作?

c# - Azure 函数在 blob 触发期间找不到 blob

c# - C# 中的 "protected"方法?

c# - 将字符串数组转换为整数数组

c# - XML 解析检查属性是否存在