我需要从下面的查询中选择 DeviceTypeID
但我不能,因为我使用 .Any()
方法开始查询。请帮我选择DeviceTypeID
var Ids = query.Any(a => a.HospitalDepartments.Any(b =>
b.Units.Any(c => c.Devices.Select(f => f.DeviceTypeID)))).ToList();
最佳答案
Any
检查集合中是否至少有一项满足谓词。从它的外观来看,您想要的是一种收集所有层次结构中所有 DeviceTypeID
的方法。如果是这样,那么您需要 SelectMany
:
var ids = query.SelectMany(a =>
a.HospitalDepartments.SelectMany(b =>
b.Units.SelectMany(c =>
c.Devices.Select(f => f.DeviceTypeID)))).ToList();
关于c# - LINQ:如何在 Any() 方法之后选择一列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43428418/