[英]C# LINQ Remove multiple objects from a list in single execution
It seems quite similar, but my question is quite different. 它似乎很相似,但我的问题却完全不同。 I have a list which consists of objects which are implementation of different interface.
我有一个列表,其中包含实现不同接口的对象。 I want to delete a particular type's objects, but after passing another criteria.
我想删除特定类型的对象,但是在传递了另一个标准之后。 A sample code is given below.
下面给出了示例代码。
// remove the not required parameters
foreach (EndPoint endPoint in endPoints)
{
var maps = endPoint.EndPointParameters
.Where(x => x is IEndPointParamMapCustom)
.Cast<IEndPointParamMapCustom>()
.Where(x => !x.IsActive)
.ToList();
foreach (var custom in maps)
{
endPoint.EndPointParameters.Remove(custom);
}
}
I want to remove the below foreach loop and remove the objects in the above single LINQ query. 我想删除下面的foreach循环并删除上面单个LINQ查询中的对象。 Is it possible?
可能吗? Thanks.
谢谢。
你可以使用RemoveRange
函数endPoint.endPointParameters.RemoveRange(maps)
If EndPointParameters
is a List<T>
("remove multiple objects from a list ") you can try RemoveAll
instead of Linq : 如果
EndPointParameters
是List<T>
(“从列表中删除多个对象”),您可以尝试RemoveAll
而不是Linq :
// Remove the not required parameters
foreach (EndPoint endPoint in endPoints)
{
// Remove all items from EndPointParameters that both
// 1. Implement IEndPointParamMapCustom
// 2. Not IsActive
endPoint
.EndPointParameters
.RemoveAll(item => (item as IEndPointParamMapCustom)?.IsActive == false);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.