代码之家  ›  专栏  ›  技术社区  ›  4est

避免覆盖ObservableCollection(C#,WPF)中的值

  •  -4
  • 4est  · 技术社区  · 7 年前

    我有工作日清单

    enter image description here

    DataGrid

    ItemsSource="{Binding Path=MyConceptItems}"
    

    然后我从json其他列表中获取,并尝试使用日期匹配:

    public ObservableCollection<MyDataConcept> MyConceptItems { get; set; }
    
    public void GetSheet(string fd, string ld){
     string period = "\"" + fd + "\",\"" + ld + "\"";
     string result = Task.Run(() => MyMethodAsync("getsheet", GetApiKeyAsync(), "," + period)).
                                    GetAwaiter().GetResult();
     MyObject resultparsed = new JavaScriptSerializer().Deserialize<MyObject>(result);
    
      foreach (var item in resultparsed.result.items) {                
       foreach (var existingItem in MyConceptItems) {
        if (existingItem.DateColumn == item.entryDate) {
                        existingItem.PColumn = item.pName;
                        existingItem.TColumn = item.aName;
                        existingItem.HSpend = item.formattedDuration;
     }}}};
    

    我的问题是:当我从json获取 然后在MyConceptItems中只显示一个(最后一个)项,可能会被覆盖。

    :

    MyConecptItems :

      Date     | PColumn
    2018-09-03 |  A2
    2018-09-04 |  B
    2018-09-05 |  C2
    

    Json :

       Date     | PName
    2018-09-03  |  A
    2018-09-03  |  A2
    2018-09-04  |  B
    2018-09-05  |  C
    2018-09-05  |  C2
    

    更新:

    foreach (var item in resultparsed.result.items) {                
       foreach (var existingItem in MyConceptItems) {
        if (existingItem.DateColumn == item.entryDate) 
         if (existingItem.IsExist == false){
                                existingItem.IsExist = true;
                                existingItem.ProjectColumn = item.projectName;
                                existingItem.TaskColumn = item.activityName;
                                existingItem.HoursSpend = item.formattedDuration;
          }
          else
          {
          MyConceptItems.Add(new MyDataConcept(item.entryDate, item.entryDayName, 
                             item.projectName, item.activityName, item.formattedDuration); 
          } 
     }}
    

    但我要传达这样一个信息:

    System.InvalidOperationException: 
    'Collection was modified; enumeration operation may not execute.'
    
    2 回复  |  直到 7 年前
        1
  •  1
  •   mm8    7 年前

    在使用 foreach 循环。尝试以下操作:

    foreach (var item in resultparsed.result.items)
    {
        var existingItem = MyConceptItems.FirstOrDefault(x => x.DateColumn == item.entryDate);
        if(existingItem != null)
        {
            //update properties...
        }
        else
        {
            //add new item
            MyConceptItems.Add(new MyDataConcept(item.entryDate, item.entryDayName,
                item.projectName, item.activityName, item.formattedDuration);
        }
    }
    
        2
  •  0
  •   slugster Joey Cai    7 年前

    您正在使用以下行对ObservableCollection进行迭代

    foreach (var existingItem in MyConceptItems) {
    

    以这种方式循环使用集合的迭代器。然后在该循环中添加一个项:

    MyConceptItems.Add(new MyDataConcept(...
    

    修改集合本身时不能使用集合枚举器(可以修改集合中某个项的属性)。

    IList 允许您指定谓词(选择特定对象的条件)的扩展方法: Contains , Any , Select SingleOrDefault