Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4045752
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T13:28:26+00:00 2026-05-20T13:28:26+00:00

Hey people yesterday i was working on some code and thought that i would

  • 0

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 ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T13:28:27+00:00Added an answer on May 20, 2026 at 1:28 pm

    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
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hey, I'm working on an iPhone app but I've heard from some people that
Hey I have this code that sends an email with some data sent by
Hey, I'm using a html form that is used to log people into my
Hey there, I'm doing some queries to a MySQL database that involves some user-input..
Hey there. I would really appreciate your help in creating a query that would
Hey people, why won't this code work? The Facebook API is so bad it's
Hey, I'm trying to write a program that will accept new tasks from people,
hey guys, i'm looking for a finished product that i can use on my
hey people.i would like to know how to develop applications for android. i know
Hey people. I see that this topic is repeated over and over on SO

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.