簡體   English   中英

LINQ 查詢 - 在列表中查找包含具有特定屬性的對象的對象

[英]LINQ Query - Find objects in a list who's lists contain objects with a certain property

我有這個結構有兩個屬性:

public struct RoomExit
{
    public RoomExitType Type;
    public bool isOccupied;
}

這個房間 object 可以有多個RoomExit結構:

public class Room : MonoBehaviour
{
    public List<RoomExit> Exits;
}

在單獨的 class 中,我有一個 Room 對象列表:

public class LevelGenerator : MonoBehaviour
{
    public List<GameObject> Rooms;
}

我想設計一個 LINQ 查詢,該查詢在List<GameObject> Rooms列表中搜索具有特定類型的Exits列表中的RoomExit的 Room 對象(比如說 RoomExitType.A)。 我認為這稱為子查詢,但我不確定。

這是我到目前為止所擁有的:

List<GameObject> SelectedRooms = Rooms.Where(x => x.GetComponent<Room>().Exits.Contains(???))

我不明白如何設計查詢的下一部分,該部分試圖查看 Exits 列表中的一個結構是否具有等於 RoomExitType.A 的 RoomExitType。 這是我正在嘗試做的視覺示意圖:

在此處輸入圖像描述

var query = Rooms.Where(room => room.Exits.Any(e => e.Type == RoomExitType.C))

您正在尋找Any() LINQ 運算符:

RoomExitType selectedType = RoomExitType.A; // Or RoomExitType.C or whatever
List<GameObject> SelectedRooms = Rooms
    .Where(x => x.GetComponent<Room>().Exits.Any(e => e.Type == selectedType)
    .ToList();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM