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 329183
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T09:33:52+00:00 2026-05-12T09:33:52+00:00

Consider the requirement to change a data member on one or more properties of

  • 0

Consider the requirement to change a data member on one or more properties of an object that is 5 or 6 levels deep.

There are sub-collections that need to be iterated through to get to the property that needs inspection & modification.

Here we’re calling a method that cleans the street address of a Employee. Since we’re changing data within the loops, the current implementation needs a for loop to prevent the exception:

Cannot assign to “someVariable” because it is a ‘foreach iteration variable’

Here’s the current algorithm (obfuscated) with nested foreach and a for.

foreach (var emp in company.internalData.Emps)
{
    foreach (var addr in emp.privateData.Addresses)
    {
        int numberAddresses = addr.Items.Length;

        for (int i = 0; i < numberAddresses; i++)
        {
            //transform this street address via a static method
            if (addr.Items[i].Type =="StreetAddress")
               addr.Items[i].Text = CleanStreetAddressLine(addr.Items[i].Text);
        }
    }
}

Question:
Can this algorithm be reimplemented using LINQ? The requirement is for the original collection to have its data changed by that static method call.

Update: I was thinking/leaning in the direction of a jQuery/selector type solution. I didn’t specifically word this question in that way. I realize that I was over-reaching on that idea (no side-effects). Thanks to everyone! If there is such a way to perform a jQuery-like selector, please let’s see it!

  • 1 1 Answer
  • 1 View
  • 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-12T09:33:52+00:00Added an answer on May 12, 2026 at 9:33 am

    LINQ is not intended to modify sets of objects. You wouldn’t expect a SELECT sql statement to modify the values of the rows being selected, would you? It helps to remember what LINQ stands for – Language INtegrated Query. Modifying objects within a linq query is, IMHO, an anti-pattern.

    Stan R.’s answer would be a better solution using a foreach loop, I think.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider the following code (from a requirement that says that 3 is special for
I want the following requirement. But there is slight change on it. http://acrisdesign.com/demo/toggle/ Please
Consider a Visual Studio 2010 project whose requirement is to model the data using
Consider the following C++ code that prints the alignment requirement for a double and
Consider the requirement to find a matched pair of set of characters, and remove
To give an idea of my requirement, consider these classes - class A {
Consider the following URL: http://www.iamsomeweiredurl.fromsomewhere/andIwant#thisfragment Is there a chance to get thisfragment by @RequestMapping
Consider the following Mechanize form object #<Mechanize::Form {name f1} {method POST} {action f.php} {fields
Let's consider that I want to extend an existing JSF component, such as the
Consider the requirement to always declare Option Strict On . We'll always need to

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.