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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T11:22:11+00:00 2026-05-12T11:22:11+00:00

I have a Linq query that looks something like this: var myPosse = from

  • 0

I have a Linq query that looks something like this:

var myPosse = from p1 in people
             select p1;
label1.Text = "All my peeps:" + Environment.NewLine;
foreach (Person p in myPosse)
{
    this.label1.Text += p.ToString() + Environment.NewLine;
}

This gives me good results.

But when I do something like this:

var myPosse = from p1 in people
             select p1;
label1.Text = "All my peeps:" + Environment.NewLine;
people.Add(new Person{FirstName="Don", LastName="Cash"});
foreach (Person p in myPosse)
{
    this.label1.Text += p.ToString() + Environment.NewLine;
}

I have the ‘extra’ guy in there! How the heck is this happening? My Linq variable is set before the extra guy is added.

  • 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-12T11:22:11+00:00Added an answer on May 12, 2026 at 11:22 am

    This is because of deferred execution, a major feature of Linq.

    What is stored in var is not actually a result set. It is actually the potential to run a query. The query isn’t run when the value is assigned to a variable. It runs only as needed and does so little by little.

    This is a huge part of Linq and is there for efficiency’s sake. Deferred execution saves lots of time and resources b/c it is possible to abort the query early and thus, we’ve wasted time and memory if we don’t need the tail end. Also, if the result set is mega-huge, it is inefficient. (Think of slurping a file versus streaming it in say, perl or PHP).

    You can’t really force immediate execution, but here’s a trick to approximate that.

    var myPosse = from p1 in people
                 select p1;
    List<Person> theTeam = myPosse.ToList();
    label1.Text = "All my peeps:" + Environment.NewLine;
    people.Add(new Person{FirstName="Don", LastName="Cash"});
    foreach (Person p in theTeam)
    {
        this.label1.Text += p.ToString() + Environment.NewLine;
    }
    

    Note the “ToList()” method. In this situation your Linq query executes in full at the .ToList() moment. Your original list is preserved in theTeam while you have your ‘extra’ guy in the people IEnumerable.

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

Sidebar

Related Questions

I have a Linq query that looks something like this: var query = from
I have a LINQ query that returns some object like this... var query =
So I have this LINQ query that ends in a custom select kinda like
I have an xml file that looks something like this <questions> <question> <text>What color
I have a LINQ query mapped with the Entity Framework that looks something like
I have a LINQ query that looks like this: public IEnumerable<Foo> SelectFooBars() { return
I have a pretty simple structure that looks something like this: var list =
I have LINQ statement that looks like this: return ( from c in customers
Say I have an entity that looks something like this simple example: MyEntity {
Okay I have something that looks like this: Company Name-1234 I only want the

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.