是否可以在Linq.Where()期间筛选子集合
本文关键字:筛选 子集合 Where Linq 是否 | 更新日期: 2023-09-27 18:26:57
我有一个对象继承:
DeathStar.Floors.Departments.Rooms
目前,我只选择那些有部门的楼层,这些部门的房间里有被机器人恐吓或注射的反叛渣滓:
var rebelScum = deathStar.Floors.Where(
f=> f.Departments.Any(
d => d.Rooms.Any(
r => r.Occupant.Allegiance == "Rebel"
&& (r.InterrogationState == Interrogation.Intimidation
|| r.InterrogationState == Interrogation.FloatyStabbyDroid)
)
)
);
然而,rebelScum
将与任何被审问的叛军渣滓在同一部门有空房间。
我可以在这个.Where()
内过滤,只返回已占用的房间吗?
IEnumerable<Room> roomsOccupiedByRebelScum = deathStar.Floors.SelectMany(f => f.Departments)
.SelectMany(d => d.Rooms)
.Where(r => r.Occupant != null && r.Occupant.Allegiance == "Rebel" &&
(r.InterrogationState == Interrogation.Intimidation || r.InterrogationState == Interrogation.FloatyStabbyDroid)
);
如果要筛选房间,它们必须形成查询的结果