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

时间:2014-03-05 13:39:25

标签: c# windows-phone-8

您好我有一个数据列表,我想从列表中删除数据,但是当我删除一次值时,我的代码返回错误是我的代码和类 错误是 收藏被修改;枚举操作可能无法执行。删除列表项

 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 个答案:

答案 0 :(得分: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 :(得分: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);
}