2 回答

TA貢獻(xiàn)1835條經(jīng)驗(yàn) 獲得超7個(gè)贊
合適的 LINQ 運(yùn)算符是.Any()
. IE
player.Inventory.Any(item => item.Attributes.Contains("Sharp"))
請(qǐng)注意,如果屬性數(shù)量變大,則性能會(huì)很差。您應(yīng)該更喜歡HashSet<string>
而不是List<string>
for Attributes
,或者Dictionary<string,int>
如果相同的屬性可以出現(xiàn)多次。

TA貢獻(xiàn)1836條經(jīng)驗(yàn) 獲得超5個(gè)贊
看起來(lái)您可以為此使用帶有 lambda 函數(shù)的 LINQ 查詢(xún)。這是一個(gè)您可以在您的 Player 類(lèi)中實(shí)現(xiàn)的函數(shù),用于在您的項(xiàng)目中查詢(xún)具有特定屬性名稱(chēng)的項(xiàng)目。
只讀解決方案 IEnumerable<Item>
public IEnumerable<Item> FindMatchingItems(string attributeName) {
return this.Items.Where(x => x.Name == attributeName).AsEnumerable();
}
列出解決方案 List<Item>
public List<Item> FindMatchingItems(string attributeName) {
return this.Items.Where(x => x.Name == attributeName).ToList();
}
- 2 回答
- 0 關(guān)注
- 412 瀏覽
添加回答
舉報(bào)