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

  • Home
  • SEARCH
  • 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 994159
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T06:32:08+00:00 2026-05-16T06:32:08+00:00

Now I’m not really sure what the problem here is but it seems that

  • 0

Now I’m not really sure what the problem here is but it seems that Linq queries are re-evaluated each iteration which is a effect I want here. However for some reason he skips elements he shouldn’t when he does so.

Can someone help me understand what’s going on here. Here is the Linq-to-XML code:

var contents = (from sdt in document.MainDocumentPart.RootElement.Descendants<SdtElement>()
                select sdt);

foreach (SdtElement item in contents)
{
    ReplaceContent(item, data);
}

The ReplaceContent method rips a part of the SdtElement out and puts it instead of that element. Next iteration contents has 1 less element as expected. However when I hit one paragraph (so 2 elements under the same parent) with 2 SdtElements and after he replaces the first one he doesn’t include the second one and skips it. It’s still in the document though.

Here is a part of the ReplaceContent method:

public void ReplaceContent(SdtElement contentControl, XElement xml)
{
    OpenXmlElement content = null;

    if (contentControl is SdtRun)
    {
        content = (contentControl as SdtRun).SdtContentRun.GetFirstChild<Run>();
        //Items cut for brevity
    }

    //Items cut for brevity
    if (content != null)
    {
         content.Remove();
         var parent = contentControl.Parent;
         parent.ReplaceChild(content, contentControl);
    }
}

I could use the ToList() method on the Linq but then I’d had to change some other code a little. That’s not really a problem I just rather would want it to work like this and also understand what it is that’s going on here.

  • 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-16T06:32:09+00:00Added an answer on May 16, 2026 at 6:32 am

    LINQ queries get re-evaluated on each use because this allows them to be minimal in memory (generally O(1) memory use) and fast to return the first result.

    In those cases where it’s not a problem, it’s by far the best way to go.

    In those cases where it is a problem, use toList() or similar to get by.

    A middle ground is to build an enumerable which passes through results for immediate processing, but which also maintains a list for subsequent work. This can be particularly useful if the first pass-through may result in the next operations being abandoned as unnecessary.

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

Sidebar

Related Questions

Now, I'm pretty sure that it's not possible, but I want to 1) Confirm
Now that LINQ to SQL is a little more mature, I'd like to know
Now this is .NET but I am sure the principal should apply to all
Now that NOSQL or object only storage systems like MongoDB or memcached are really
Now that most of the major browsers support full page zoom (at present, the
Now that I know C++ I want to get into desktop application that have
Now that .NET v3.5 SP1 has been released (along with VS2008 SP1), we now
Now that it's clear what a metaclass is , there is an associated concept
Now that everyone is talking about MVC, I notice that the business rules are
now i get value if i selected checkbox(id which was selected), but how get

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.