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 ?
Because the next iteration will make your variable
currentCellnull again whenFinddidn’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: