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.
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.
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.