如何根据单个属性值删除项目 [英] How to remove a item based on single property value

查看:67
本文介绍了如何根据单个属性值删除项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个List *< plantcollection> _PlantCollection列表包含5个属性。



I have a List* <plantcollection> _PlantCollection list which contains 5 properties.

class PlantCollection
    {
        private readonly Parser cParser = new Parser();
        public string Name { get; set; }
        public string SafeToFeed { get; set; }
        public string URLOfPlant { get; set; }
        public BitmapImage PlantThumbnail { get; set;}
        public string PlantTrafficColour { get; set; }
}





如何根据单个值的条件从列表中删除项目?



例如



如果安全饲料包含(不喂食)..删除项目。我尝试使用项目的索引,但它只返回-1。



我试过这个但是返回一个异常 - 集合已被修改





How would I remove a item from the list based on a condition of a single value?

E.g

If Safe To Feed contains ("Do Not Feed").. remove item. I tried using an the index of the item but it just return as -1.

I tried this but returns an exception - "collection has been modified"

int i = 0;
foreach (PlantCollection plant in cParser._PlantCollection)
{
    if (cParser._PlantCollection[i].SafeToFeed.Contains("green"))
    {
        cParser._PlantCollection.Remove(plant);
        i++;
    }


}









推荐答案

我会做类似的事情



I would do something similar to this

//Find all items that contain "Do Not Feed"
List<plantcollection> DataToRemove = Container.PlantCollection.FindAll(p=> p.SafeToFeed.Contains("Do Not Feed") == true);

if(DataToRemove != null)
{
  foreach(PlantCollection item in DataToRemove)
  {
    Container.PlantCollection.Remove(item);
  }
}


这篇关于如何根据单个属性值删除项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆