我有一个集合称为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'。 –