-2

using linq I want to check certain condition, if that condition is met I want to remove that object from the list

pseudo code

if any object inside cars list has Manufacturer.CarFormat != null
delete that object

if (muObj.Cars.Any(x => x.Manufacturer.CarFormat != null))
{
    ?
}
0

2 Answers 2

4

using the List function RemoveAll, you can

muObj.Cars.RemoveAll(x => x.Manufacturer.CarFormat != null);
Sign up to request clarification or add additional context in comments.

2 Comments

Technically that's not Linq - just a normal method on List.
yep you're right. edited.
1

I don't have this RemoveAll method on IList

That's because RemoveAll is a method on List<T>, not IList<T>. If you don't want to try casting to List<T> (what if it fails?) then one option is loop through by index (in reverse order so as to not mess up the index counting:

for (int i = muObj.Cars.Count - 1; i >= 0; i--)
{
    if(muObj.Cars[i].Manufacturer.CarFormat != null)
        muObj.Cars.RemoveAt(i);
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.