我有以下接口列表:
List<IInterfaces> instantiatedInterfaces;
IInterfaces
具有以下属性:
List<StandardPort> ListOfPorts { get; }
UInt16 NumberOfPorts { get; }
StandardPort
具有以下属性:
public Uint16 Side { get; set; }
现在,假设instantiatedInterfaces
已正确填充,我如何才能将List<StandardPort>
的所有端口(对于特定的ListOfPorts
)提取到Side
中instantiatedInterfaces
我尝试过的方法(不起作用-返回一个空列表):
List<StandardPort> foundPorts = instantiatedInterfaces.Select(i => i.ListOfPorts.Where(p => p.side == Left)) as List<StandardPort>;
答案 0 :(得分:3)
您正在寻找SelectMany()
SHA Hash
答案 1 :(得分:2)
List<StandardPort> ports = instantiatedInterfaces
.SelectMany(intf => intf.ListOfPorts)
.Where(port => port.side == Left)
.ToList();
答案 2 :(得分:2)
您可以使用Enumerable.SelectMany
将“列表列表”展平为一个列表:
var allPorts = instantiatedInterfaces.SelectMany(iface => iface.ListOfPorts);
然后您可以使用Enumerable.Where
过滤此列表:
var foundPorts = allPorts.Where(port => port.Side == Left).ToList();