简体   繁体   English

如何仅从某些属性设置为true的列表中选择项目

[英]How to only select items from a list with certain property set to true

I have a Collection called ItemCollection that looks like: 我有一个名为ItemCollection的集合,看起来像:

public class ItemCollection : List<Item>
{
}

The Item has a property called MyProperty : Item具有一个名为MyProperty的属性:

public class Item
{
    public bool MyProperty { get; set; }
}

I also have a ItemManager that has a GetItems method which returns an ItemCollection . 我也有一个具有GetItems方法的ItemManager ,该方法返回一个ItemCollection

Now I want to only get items from my ItemCollection with MyProperty set to true. 现在,我只想从MyProperty设置为true的ItemCollection获取项目。

I tried: 我试过了:

ItemCollection ic = ItemManager.GetItems().Where(i => i.MyProperty);

Unfortunately the Where part does not work. 不幸的是, Where部分不起作用。 Although i refers to an Item I get the error 虽然i指的是Item我得到了错误

Cannot implicitly convert type Item to ItemCollection. 无法将类型Item隐式转换为ItemCollection。

How can I filter the returned ItemCollection to only contain those Item s that have MyProperty set to true? 如何过滤返回的ItemCollection以仅包含MyProperty设置为true的那些Item

Some of the answers/comments have mentioned 提到了一些答案/评论

(ItemCollection)ItemManager.GetItems().Where(i => i.MyProperty).ToList()

which will not work because of up-casting. 由于无法正常播放,因此无法使用。 Instead, the above will produce a List<Item> . 而是上面的代码将产生List<Item>

The following is what you will need to make these work. 以下是使这些工作所需的条件。 Note that you will need to have the ability to modify the ItemCollection class in order for this to work. 请注意,您将需要具有修改ItemCollection类的功能,以便此功能能够起作用。


Constructor 构造函数

If you would like to make a constructor for the ItemCollection class, then the following should work: 如果您想为ItemCollection类构造一个构造函数,那么以下方法应该起作用:

public ItemCollection(IEnumerable<Item> items) : base(items) {}

To call the constructor, you would then do the following: 要调用构造函数,您需要执行以下操作:

var ic = new ItemCollection(ItemManager.GetItems().Where(i => i.MyProperty));

or 要么

ItemCollection ic = new ItemCollection(ItemManager.GetItems().Where(i => i.MyProperty));


Note about the error message 关于错误消息的注意

In the comments, when asked to change ItemCollection ic = ItemManager.GetItems..... to var ic = ItemManager.GetItems..... and then tell us what the type of ic is, you mentioned that you got Systems.Collections.Generic.List<T> which would translate to List<Item> . 在注释中,当要求将ItemCollection ic = ItemManager.GetItems.....更改为var ic = ItemManager.GetItems..... ,然后告诉我们ic的类型是什么时,您提到获得了Systems.Collections.Generic.List<T>会转换为List<Item> The error message that you received was actually not the error message that you should have received, which is likely just due to the IDE being confused, which occasionally happens when there are errors on the page. 您收到的错误消息实际上不是您应该收到的错误消息,这很可能是由于IDE混淆所致,有时页面上有错误时会发生这种情况。 What you should have received is something more along the lines of: 您应该收到的是以下内容:

Cannot implicitly convert type IEnumerable<Item> to ItemCollection.

Extension functions are Great solution too : 扩展功能也是很好的解决方案:

public static class Dummy { 
    public static ItemCollection ToItemCollection(this IEnumerable<Item> Items)
    {
        var ic = new ItemCollection();
        ic.AddRange(Items);
        return ic;
    }
}

So you get your result by: 因此,您可以通过以下方式获得结果:

ItemCollection ic = ItemManager.GetItems().Where(i => i.MyProperty).ToItemCollection();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM