第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

在所有類(lèi) List<Object> 中搜索匹配值

在所有類(lèi) List<Object> 中搜索匹配值

C#
紅顏莎娜 2021-11-14 17:04:11
我有兩個(gè)對(duì)象,一個(gè)引用另一個(gè)。我希望能夠使用類(lèi)似于Player.Inventory.Contain(Item.Attributes == "Sharp"). 我的目標(biāo)是能夠掃描所有物品屬性的玩家?guī)齑?,并檢查是否有一個(gè)或多個(gè)或沒(méi)有匹配。通過(guò)這種方式,我可以根據(jù)角色庫(kù)存動(dòng)態(tài)改變發(fā)生的事情。class Player{    public string Name { get; set; }    public List<Item> Inventory { get; set; }    public Player()    {        Inventory = new List<Item>();    }}和:public class Item{    public int ID { get; set; }    public string Name { get; set; }    public bool IsCarried { get; set; }    public List<string> Attributes { get; set; }    public Item(int id, string name)    {        ID = id;        Name = name;        Attributes = new List<string>();    }    public Item(int id, string name, bool iscarried)    {        ID = id;        Name = name;        IsCarried = iscarried;        Attributes = new List<string>();    }}
查看完整描述

2 回答

?
qq_花開(kāi)花謝_0

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)多次。


查看完整回答
反對(duì) 回復(fù) 2021-11-14
?
一只甜甜圈

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();

}


查看完整回答
反對(duì) 回復(fù) 2021-11-14
  • 2 回答
  • 0 關(guān)注
  • 412 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢(xún)優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)