技术内部:为什么这次迭代找不到我的项目

发布于 2024-10-21 20:28:45 字数 1243 浏览 2 评论 0原文

嘿人们 昨天我正在编写一些代码,并认为我会保存 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

俯瞰星空 2024-10-28 20:28:45

因为当 Find 没有找到具有匹配 ID 的单元格时,下一次迭代将使您的变量 currentCell 再次为 null。在第二段代码中,您立即返回找到的单元格。

在第一段代码中尝试以下操作:

foreach (XmlRow row in block.Rows)
{
 currentCell = row.Cells.Find(cell => string.Equals(cell.Id, id));
 //new
 if (currentCell != null)
  return currentCell;
 //end new
}

Because the next iteration will make your variable currentCell null again when Find 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:

foreach (XmlRow row in block.Rows)
{
 currentCell = row.Cells.Find(cell => string.Equals(cell.Id, id));
 //new
 if (currentCell != null)
  return currentCell;
 //end new
}
为你拒绝所有暧昧 2024-10-28 20:28:45

当您找到具有指定 ID 的单元格时,currentCell 将设置为此特定对象。但是,在 foreach 循环的下一次迭代中,currentCell 设置为空,因为 Find 不返回任何内容:)

 foreach (XmlRow row in block.Rows)
  {
     currentCell = row.Cells.Find(cell => string.Equals(cell.Id, id));
  }

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, because Find returns nothing :)

 foreach (XmlRow row in block.Rows)
  {
     currentCell = row.Cells.Find(cell => string.Equals(cell.Id, id));
  }
感情旳空白 2024-10-28 20:28:45

代码的两个版本是不同的 - 第二个版本一旦找到匹配的单元格就会从所有嵌套循环返回,而第一个版本则返回最后一个块的最后一行中是否存在该单元格。

修复 - 添加检查以查找内循环中的单元格并在找到后立即重新运行:

foreach (XmlBlock block in section.Blocks)        
{  
    foreach (XmlRow row in block.Rows)             
    {                 
         currentCell = row.Cells.FirstOrDefault(cell => string.Equals(cell.Id, id));             
         if (currentCell != null) 
           return currentCell;

    }
} 

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:

foreach (XmlBlock block in section.Blocks)        
{  
    foreach (XmlRow row in block.Rows)             
    {                 
         currentCell = row.Cells.FirstOrDefault(cell => string.Equals(cell.Id, id));             
         if (currentCell != null) 
           return currentCell;

    }
} 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文