0

I have a model which contains a list and I need to remove/delete all items from the model based on the MarkedForDeletion value.

So far I've tried the following but this is not valid, I would prefer to not loop through the records using a for loop.

Models cs

public class CategoryModel
{
    public string Label { get; set; }
    public List<ItemModel> Items { get; set; }
}

public class ItemModel
{
    public string Label { get; set; }
    public bool MarkedForDeletion { get; set; )
}

Attempted Solution (not a valid statement)

CategoryModel.Items.Where(x => x.MarkedForDeletion = true).Remove;

1 Answer 1

3

Have you tried RemoveAll() which will remove all the elements that match the conditions defined by the specified predicate?

CategoryModel.Items.RemoveAll(x => x.MarkedForDeletion);
Sign up to request clarification or add additional context in comments.

2 Comments

I had tried variations with remove and removeall but can't have tried that as this works now. Thanks
@Fletch: Glad to hear that, happy codding..! :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.