如果某些语句为真,则从列表中删除对象
remove object from the list if certain statement is true
使用 linq 我想检查特定条件,如果满足该条件我想从列表中删除该对象
伪代码
if any object inside cars list has Manufacturer.CarFormat != null
delete that object
if (muObj.Cars.Any(x => x.Manufacturer.CarFormat != null))
{
?
}
使用列表函数 RemoveAll,您可以
muObj.Cars.RemoveAll(x => x.Manufacturer.CarFormat != null);
I don't have this RemoveAll
method on IList
那是因为 RemoveAll
是 List<T>
上的方法,而不是 IList<T>
上的方法。如果您不想尝试强制转换为 List<T>
(如果失败怎么办?),那么一个选项是按索引循环(以相反的顺序以免弄乱索引计数:
for (int i = muObj.Cars.Count - 1; i >= 0; i--)
{
if(muObj.Cars[i].Manufacturer.CarFormat != null)
muObj.Cars.RemoveAt(i);
}
使用 linq 我想检查特定条件,如果满足该条件我想从列表中删除该对象
伪代码
if any object inside cars list has Manufacturer.CarFormat != null
delete that object
if (muObj.Cars.Any(x => x.Manufacturer.CarFormat != null))
{
?
}
使用列表函数 RemoveAll,您可以
muObj.Cars.RemoveAll(x => x.Manufacturer.CarFormat != null);
I don't have this
RemoveAll
method onIList
那是因为 RemoveAll
是 List<T>
上的方法,而不是 IList<T>
上的方法。如果您不想尝试强制转换为 List<T>
(如果失败怎么办?),那么一个选项是按索引循环(以相反的顺序以免弄乱索引计数:
for (int i = muObj.Cars.Count - 1; i >= 0; i--)
{
if(muObj.Cars[i].Manufacturer.CarFormat != null)
muObj.Cars.RemoveAt(i);
}