简体   繁体   English

Asp.net 如何从 Model 中删除特定列表项

[英]Asp.net how to Delete specific List Items from a Model

I have a model which contains a list and I need to remove/delete all items from the model based on the MarkedForDeletion value.我有一个 model 包含一个列表,我需要根据MarkedForDeletion值从 model 中删除/删除所有项目。

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.到目前为止,我已经尝试了以下方法,但这无效,我宁愿不使用 for 循环遍历记录。

Models cs型号 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;

Have you tried RemoveAll() which will remove all the elements that match the conditions defined by the specified predicate?您是否尝试过RemoveAll()将删除与指定谓词定义的条件匹配的所有元素?

CategoryModel.Items.RemoveAll(x => x.MarkedForDeletion);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM