【问题标题】:Trying to change an element in a Collection尝试更改集合中的元素
【发布时间】:2021-04-22 00:45:43
【问题描述】:

我正在尝试修改此代码。我需要检查 FilteredCheckObservable 的特定元素中的值,如果为真,则更改该元素另一部分的值。

基本上是这样的

if (FilteredCheckObservable items.Lang = 'ENG')
    {items.check = newcheckname;}

然后这将更新 sourceGroups 集合。

    if (string.IsNullOrEmpty(departmentLine.LineID) || string.IsNullOrEmpty(departmentLine.LineName))
        continue;
        
    bool discovered = false;
    foreach (var group in sourceGroups)
    {
         if (!group.Key.Line.IsEqual(departmentLine))
             continue;
         group.Key.IsDiscovered = true;
         discovered = true;
         group.Key.ScheduleStatusCount = group.CountGroupItem;
         break;
    }
    if (discovered == false)
    {
        var _ScheduleItemObservable = new ScheduleItemObservable(departmentLine, MainViewViewModel, Shift.ToString());
                    
        var item = new Grouping<ScheduleItemObservable, FilteredCheckObservable>(_ScheduleItemObservable);
        if (IsShiftValid)
        {
            item.Key.Shift = Shift.ToString();
            item.Key.IsHistoryEnabled = true;
        }
        sourceGroups.Add(item);                     
    }
for (int index = 0; index < sourceGroups.Count; index++)
                        {
                            if (sourceGroups[index].Key.IsDiscovered == true)
                            {
                                foreach (var group in sourceGroups)
                                {
                                    foreach (FilteredCheckObservable items in group)
                                    {
                                        if (items.Lang_ID == LanguageService.Instance.LanguageType.ToString())
                                        {
                                            sourceGroups.Clear();
                                            sourceGroups.Add(item);
                                        }
                                    }
                                }
                            }
                        }

【问题讨论】:

  • 为了清楚起见,您能否只保留与该问题相关的代码,让不知道您的代码的人在不知道上下文和链接的情况下看到一堆代码可能会感到困惑之间。
  • 我很抱歉。第一个 sn-p 是通过 sourceGroups.Add() 构建集合的功能代码。第二次 sn-p 是我操纵收藏的糟糕尝试。感谢您的帮助。
  • 这是一个通用的 C# 问题,与 Xamarin 无关

标签: c# observablecollection


【解决方案1】:

欢迎特拉维斯,

我敢打赌,罪魁祸首是你的 foreach 循环。 https://stackoverflow.com/a/759985/3403999

您不能在 foreach 循环中修改集合。 我不知道您的集合是否有索引器,但如果有,您可以转换为 for 循环:

for (int i = 0; i < sourceGroups.Count; i++)
{
    var group = sourceGroups[i];
    // The rest of your code.

我可能是错的。您确实说您正在尝试修改一些现有代码。是网上的一些代码吗?也许您可以链接到它以提供完整的上下文。

根据您的新 sn-p,您需要两个 for 循环:

for (int index = 0; index < sourceGroups.Count; index++)
{
    if (sourceGroups[index].Key.IsDiscovered == true)
    {
        //foreach (var group in sourceGroups)
    for (int j = 0; j < sourceGroups.Count; j++)
        {
            var group = sourceGroups[j];
            foreach (FilteredCheckObservable items in group)
            {
                if (items.Lang_ID == LanguageService.Instance.LanguageType.ToString())
                {
                    sourceGroups.Clear();
                    sourceGroups.Add(item);
                }
            }
        }
    }
}

^ 虽然这可能仍然是一个糟糕的循环。主要是因为你有sourceGroups.Clear();

最好的做法是创建一个名为 say 'results' 的内部集合。循环查找您的条件,如果满足,则将该项目添加到结果集合中。

一旦你的循环终止,然后调用 sourceGroups.Clear(),然后调用 sourceGroups.AddRange(results)。如果 sourceGroups 没有 AddRange,则最后一个循环:

foreach (var group in results) { sourceGroups.Add(group); }

【讨论】:

  • 感谢您的快速回复。我引用的代码在构建初始集合时正在工作。我需要做的是在一个按钮事件上,回到这个集合并更改部分值。我实际上有另一段代码在尝试这个......我会在这里复制:
  • 我添加了我正在尝试做的代码的第二个 sn-p。
  • @Travis sourceGroups.Clear(); sourceGroups.Add(item);
  • 因此,通过您的“Clear()”调用,充其量在完成所有操作后,您的收藏中应该只有一个项目是最后一次成功匹配。此外,如果您确实在使用 ObservableCollection,则每次调用 .Clear 和 .Add 时,它都会触发事件,这将是一个昂贵的过程。最好弄清楚要在内部集合中添加哪些项目,然后调用“AddRange”。这将允许您添加所有项目,最后只触发一个事件。
  • 如何建立内部结果列表?
猜你喜欢
  • 1970-01-01
  • 2012-03-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多