技术内部:为什么这次迭代找不到我的项目
嘿人们 昨天我正在编写一些代码,并认为我会保存 foreach 迭代并在 List<> 上使用 find 扩展。反对,但我从来没有让它发挥作用。后来我重构了代码,使用 foreach 和 lamda where 语句,每次都能找到我的项目。
我的2个版本的代码发布在下面:
不工作版本:
private XmlCell FindCell(string id)
{
XmlSection section = new XmlSection(SectionNode);
XmlCell currentCell = null;
foreach (XmlBlock block in section.Blocks)
{
foreach (XmlRow row in block.Rows)
{
currentCell = row.Cells.Find(cell => string.Equals(cell.Id, id));
}
}
Assert.IsNotNull(currentCell, string.Format("Cell with id {0} not found", id));
return currentCell;
}
工作版本:
XmlSection section = new XmlSection(SectionNode);
XmlCell currentCell = null;
foreach (XmlBlock block in section.Blocks)
{
foreach (XmlRow row in block.Rows)
{
foreach (XmlCell cell in row.Cells.Where(cell => string.Equals(cell.Id, id)))
{
return cell;
}
}
}
Assert.IsNotNull(currentCell, string.Format("Cell with id {0} not found", id));
return currentCell;
有人可以解释一下为什么第一段代码没有完成工作,msdn说find方法返回它在列表中找到的第一个实例。
我什至遇到了一些“集合已修改;枚举操作可能无法执行”错误,为什么?
Hey people
yesterday i was working on some code and thought that i would save a foreach iteration and use the find extension on the List<> object, but i never got it to work. later i refactored the the code to use a foreach with a lamda where statement and that found my item evrytime.
my 2 versions of code are posted below:
Not working version:
private XmlCell FindCell(string id)
{
XmlSection section = new XmlSection(SectionNode);
XmlCell currentCell = null;
foreach (XmlBlock block in section.Blocks)
{
foreach (XmlRow row in block.Rows)
{
currentCell = row.Cells.Find(cell => string.Equals(cell.Id, id));
}
}
Assert.IsNotNull(currentCell, string.Format("Cell with id {0} not found", id));
return currentCell;
}
working version :
XmlSection section = new XmlSection(SectionNode);
XmlCell currentCell = null;
foreach (XmlBlock block in section.Blocks)
{
foreach (XmlRow row in block.Rows)
{
foreach (XmlCell cell in row.Cells.Where(cell => string.Equals(cell.Id, id)))
{
return cell;
}
}
}
Assert.IsNotNull(currentCell, string.Format("Cell with id {0} not found", id));
return currentCell;
could someone plx explain why the first bit of code dont get the job done, msdn says that the find method returns the first instance it finds in the list.
i even encountered some "Collection was modified; enumeration operation may not execute" errors, why ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
因为当
Find
没有找到具有匹配 ID 的单元格时,下一次迭代将使您的变量currentCell
再次为 null。在第二段代码中,您立即返回找到的单元格。在第一段代码中尝试以下操作:
Because the next iteration will make your variable
currentCell
null again whenFind
didn't find the cell with the matching ID. In the second piece of code you return the found cell immediately.In the first piece of code try this:
当您找到具有指定 ID 的单元格时,
currentCell
将设置为此特定对象。但是,在 foreach 循环的下一次迭代中,currentCell
设置为空,因为Find
不返回任何内容:)When you find your cell with the specified ID, the
currentCell
is set to this specific object. However, on the next iteration in the foreach loop,currentCell
is set to nothing, becauseFind
returns nothing :)代码的两个版本是不同的 - 第二个版本一旦找到匹配的单元格就会从所有嵌套循环返回,而第一个版本则返回最后一个块的最后一行中是否存在该单元格。
修复 - 添加检查以查找内循环中的单元格并在找到后立即重新运行:
The 2 versions of code are different - second one returns from all nested loops as soon as it finds a matching cell, while first one returns presence of the cell in the last row of the last block.
Fix - add check for finding the cell in inner loop and retrun as soon one found: