我有一個集合稱爲ItemCollection
看起來像選擇項目:如何只從具有特定屬性列表設置爲true
public class ItemCollection : List<Item>
{
}
的Item
有一個名爲MyProperty
屬性:
public class Item
{
public bool MyProperty { get; set; }
}
我還有一個ItemManager
,它有一個GetItems
方法返回ItemCollection
。
現在我只想從我的ItemCollection
中獲得項目,並將MyProperty
設置爲true。
我想:
ItemCollection ic = ItemManager.GetItems().Where(i => i.MyProperty);
不幸的是,Where
部分不工作。雖然i
指Item
我得到的錯誤
無法項目類型隱式轉換到ItemCollection。
我如何篩選返回ItemCollection
到只包含那些Item
S作MyProperty
設置爲true?
的:
所以您得到您的結果部分可能是好的,但返回的值是一個IEnumerable- ,不能分配給類型爲'ItemCollection'的'ic' –
這是確切的代碼?我沒有看到任何試圖將「Item」轉換爲「ItemCollection」的東西。 –
該錯誤似乎表明您正在使用'First','Single'等而不是'Where'。 –