从列表中foreach循环返回错误删除项目

问题描述:

你好,我有一个数据表,我想从列表中删除的数据,但我的代码返回,当我删除值一个时间,这里是我的代码和类 的一个错误错误是 收集已修改;枚举操作可能不会执行。删除列表项从列表中foreach循环返回错误删除项目

boko_data_json ListAvailableData = Newtonsoft.Json.JsonConvert.DeserializeObject<boko_data_json>(json); 


     foreach (var item in ListAvailableData.data) 
     { 

      string PDFPath = item.downloadpdfpath; 

      string filename = lastPart.Split('.')[0]; 
      int result = obj.getfile(filename); 
      if (result == 1) 
      { 
       ListAvailableData.data.Remove(item); 

      } 
     } 

     listnameAvailable.ItemsSource = ListAvailableData.data; 



    } public class boko_data_json 
{ 
    // public string Type { get; set; } 
    public List<Book> data{ get; set; } 

    public string downloadpdfpath { get; set; } 

    public string book_name { get; set; } 
} 

public class Book 
{ 
    public int book_id { get; set; } 
    public string book_name { get; set; } 
    public string issue_date { get; set; } 
    public string description { get; set; } 
    public string status { get; set; } 
    public string month { get; set; } 
    public int price { get; set; } 
    private string forprice { get { return "TL"; } } 
    public string showprice { get { return price +" "+forprice; } } 
    private string staticpath { get { return "http://dergiapp.net/"; } } 
    public string book_image { get; set;} 
    public string imagepath {get {return staticpath+book_image; }} 

    public string pdf_path { get; set; } 
    public string staticpdfpath { get { return "http://dergiapp.net/mobile/test.php?file="; } } 
    public string downloadpdfpath { get { return staticpdfpath + pdf_path; } } 

    private string Privewpadf1 { get { return "http://dergiapp.net/zip/p"; } } 
    private string Privewpadf2 { get { return ".zip"; } } 
    public string privewpdf { get { return Privewpadf1 + book_id + Privewpadf2; } } 

    public string download_status { get; set; } 


} 
+2

您不能从正在由的的foreach – AbZy

+0

可能重复列举的清单中删除项目[如何有条件地从.NET集合中删除项目](http://*.com/questions/653596/how-to-conditionally-remove-items-from-a-net-collection) –

您应该使用List.RemoveAll()方法,这个代码片段说明删除所有匹配特定谓词的元素,:

List<string> strList = new List<string>() 
{ 
    "One", 
    "Two", 
    "RemoveMe", 
    "Three", 
    "Four" 
}; 

strList.RemoveAll(element => element == "RemoveMe"); 

这将删除匹配“RemoveMe”的所有元素。

如果断言是相当复杂的,你可以把它变成一个独立的方法,像这样:

strList.RemoveAll(shouldBeRemoved); 

... 

private static bool shouldBeRemoved(string element) 
{ 
    // Put whatever complex logic you want here, 
    // and return true or false as appropriate. 

    return element.StartsWith("Remove"); 
} 

,而你的循环通过它的项目你不能从你的列表中删除的项目。您正在修改集合的内容,同时有一个枚举它的循环。 这就是Collection was modified; enumeration operation may not execute. removing list item的原因。

你应该做到以下几点:

boko_data_json copyList = ListAvailableData; 

foreach (var item in ListAvailableData.data) 
     { 
      string PDFPath = item.downloadpdfpath; 
      string filename = lastPart.Split('.')[0]; 
      int result = obj.getfile(filename); 
      if (result == 1) 
      { 
       copyList.data.Remove(item); 
      } 
     } 

listnameAvailable.ItemsSource = copyList.data; 

另一种方法是这样的:

boko_data_json itemsToRemove = new boko_data_json(); 

foreach (var item in ListAvailableData.data) 
      { 
       string PDFPath = item.downloadpdfpath; 
       string filename = lastPart.Split('.')[0]; 
       int result = obj.getfile(filename); 
       if (result == 1) 
       { 
        itemsToRemove.data.Add(item); 
       } 
      } 

foreach (var itemToRemove in itemsToRemove) 
{ 
    ListAvailableData.data.Remove(itemToRemove); 
}