remove object from the list if certain statement is true
本问题已经有最佳答案,请猛点这里访问。
使用linq我想检查某些条件,如果满足该条件,我想从列表中删除该对象。
伪码
1 2 3 4 5 6 7 | if any object inside cars list has Manufacturer.CarFormat != null delete that object if (muObj.Cars.Any(x => x.Manufacturer.CarFormat != null)) { ? } |
使用list函数removeall,可以
1 | muObj.Cars.RemoveAll(x => x.Manufacturer.CarFormat != null); |
I don't have this
RemoveAll method onIList
这是因为
1 2 3 4 5 | for (int i = muObj.Cars.Count - 1; i >= 0; i--) { if(muObj.Cars[i].Manufacturer.CarFormat != null) muObj.Cars.RemoveAt(i); } |