尝试更改集合中的元素
Trying to change an element in a Collection
我正在尝试修改此代码。我需要检查 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);
}
}
}
}
}
欢迎特拉维斯,
我敢打赌罪魁祸首是你的 foreach 循环。
https://whosebug.com/a/759985/3403999
您不能在 foreach 循环中修改 collection。
我不知道你的 collection 是否有索引器,但如果有,你可以转换为 for 循环:
for (int i = 0; i < sourceGroups.Count; i++)
{
var group = sourceGroups[i];
// The rest of your code.
我可能是错的。您确实说您正在尝试修改一些现有代码。是在线的一些代码吗?也许您可以 link 以提供完整的上下文。
根据您的新代码段,您需要两个 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' 的内部 collection。做你的循环寻找你的条件,如果他们满足,将该项目添加到结果 collection.
循环终止后,调用 sourceGroups.Clear(),然后调用 sourceGroups.AddRange(results)。如果 sourceGroups 没有 AddRange,那么最后一个循环:
foreach (var group in results) { sourceGroups.Add(group); }
我正在尝试修改此代码。我需要检查 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);
}
}
}
}
}
欢迎特拉维斯,
我敢打赌罪魁祸首是你的 foreach 循环。 https://whosebug.com/a/759985/3403999
您不能在 foreach 循环中修改 collection。 我不知道你的 collection 是否有索引器,但如果有,你可以转换为 for 循环:
for (int i = 0; i < sourceGroups.Count; i++)
{
var group = sourceGroups[i];
// The rest of your code.
我可能是错的。您确实说您正在尝试修改一些现有代码。是在线的一些代码吗?也许您可以 link 以提供完整的上下文。
根据您的新代码段,您需要两个 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' 的内部 collection。做你的循环寻找你的条件,如果他们满足,将该项目添加到结果 collection.
循环终止后,调用 sourceGroups.Clear(),然后调用 sourceGroups.AddRange(results)。如果 sourceGroups 没有 AddRange,那么最后一个循环:
foreach (var group in results) { sourceGroups.Add(group); }