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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T20:30:03+00:00 2026-06-04T20:30:03+00:00

I tried this simple query on LinqPad and scratched by head as to why

  • 0

I tried this simple query on LinqPad and scratched by head as to why the second query works but the first one just does not alter the list(descen).

  • How do i make it work. I use this code to clone and modify xml attribute,value

Fails

var doc = XElement.Parse("<Root><Descendants>Welcome</Descendants><Descendants>Stack</Descendants><Descendants>Overflow</Descendants></Root>");
var descen = (from des in doc.Descendants("Descendants") select new XElement(des));
foreach (var desc in descen)
{
    desc.Value += DateTime.UtcNow;
}
descen.Dump();
doc.Dump();

Works

var doc = XElement.Parse("<Root><Descendants>Welcome</Descendants><Descendants>Stack</Descendants><Descendants>Overflow</Descendants></Root>");
var descen = (from des in doc.Descendants("Descendants") select new XElement(des));
foreach (var desc in doc.Descendants("Descendants"))
{
    desc.Value += DateTime.UtcNow;
}
descen.Dump();
doc.Dump();

Stall’s my PC ?? WTH

var doc = XElement.Parse("<Root><Descendants>Welcome</Descendants><Descendants>Stack</Descendants><Descendants>Overflow</Descendants></Root>");
var descen = (from des in doc.Descendants("Descendants") select des);
foreach (var desc in descen)
{
    desc.Value += DateTime.UtcNow;
}
var instance = from t in descen select new XElement(t);
doc.Elements("Descendants").LastOrDefault().AddAfterSelf(instance);
descen.Dump();
  • 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-06-04T20:30:04+00:00Added an answer on June 4, 2026 at 8:30 pm

    In the first query, descen is a projection from existing elements to new elements. It’s lazily evaluated – each time you iterate over it, it will create new elements.

    In the first case, you iterate over descen, and modify the new elements as you go. However, those modified elements are effectively thrown away very soon after they’re created – they’re not part of the original document.

    In the second case, you modify the elements in the original document, so when you iterate over descen, it will create a copy of the modified elements, and displays those.

    EDIT: If you want to change descen but not the original document, you can just add ToList() at the end of the initialization part of descen. For example:

    var descen = (from des in doc.Descendants("Descendants")
                  select new XElement(des)).ToList();
    

    Or, more readably IMO:

    var descen = doc.Descendants("Descendants")
                    .Select(x => new XElement(x))
                    .ToList();
    

    The reason your final code hangs is that as AddAfterSelf iterates over instance, it’s adding elements… but those elements are then part of the document, which means they’re part of the descen query, which means they’re part of the instance query… so iterating over instance will never complete.

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

Sidebar

Related Questions

Im stumped by this simple query because its one I have not tried before.
This task should be quite simple, but nothing I tried has worked. I'm just
I am tired to find out why this simple query not working. SELECT image_url
This should be really simple, but I have tried many solutions posted on the
I did a very simple query yesterday but this morning I couldn't remember how
I tried this as a simple test: @functions { private MvcHtmlString helloWorld() { return
I have tried to boil this down to a simple example to demonstrate what
I have started using Simple-form and Bootstrap and I have tried to follow this
i tried to do a simple ksoap2 tutorial. This is the link My Problem
Sorry, I'm sure this is simple but I'm tired and can't figure it out.

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.