简体   繁体   English

从linq结果集中删除项目

[英]Removing Items from linq resultset

I am using the following code: 我正在使用以下代码:

var images = from pic in slidePart.Slide.Descendants<DocumentFormat.OpenXml.Presentation.Picture>()
             select pic;

foreach (var image in images)
       image.Remove();

The problem is with the loop which executing only once and images contain multiple objects. 问题在于循环仅执行一次并且图像包含多个对象。 How can I call image.Remove on all objects. 如何调用图片。移除所有对象。

Try this: 尝试这个:

slidePart
    .Slide
    .Descendants<DocumentFormat.OpenXml.Presentation.Picture>()
    .ToList()
    .ForEach(pic => pic.Remove());

Why the .ToList() call? 为什么要调用.ToList() Because you need to ensure that you don't modify the collection you are iterating over. 因为您需要确保不修改集合,所以要进行迭代。

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

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