2014-03-05 158 views
0

你好,我有一個數據表,我想從列表中刪除的數據,但我的代碼返回,當我刪除值一個時間,這裏是我的代碼和類 的一個錯誤錯誤是 收集已修改;枚舉操作可能不會執行。刪除列表項從列表中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://stackoverflow.com/questions/653596/how-to-conditionally-remove-items-from-a-net-collection) –

回答

6

您應該使用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"); 
} 
1

,而你的循環通過它的項目你不能從你的列表中刪除的項目。您正在修改集合的內容,同時有一個枚舉它的循環。 這就是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); 
}