If I use any integer variable in a LINQ where
clause, and after query execution if I modify / initialize the integer variable, then the result set of the LINQ query changes. For example:
static void Main(string[] args)
{
int startPos = 0;
List<string> intList = new List<string>();
for (int i = 0; i < 10; i++)
{
intList.Add(i.ToString());
}
var qResult = from c in intList
where Convert.ToInt32(c) >= startPos
select c;
// prints count as 10
Console.WriteLine("List count is :"+qResult.Count());
startPos = 5;
// prints count as 5
Console.WriteLine("List count is :" + qResult.Count());
}
Output:
List count is :10
List count is :5
In above example you can see that after startPos = 5;
, the qResult
has changed. I don't understand how this has happened; as far as I know, int
is value type.
Is the Linq query again executed at line startPos = 5;
? If yes, then will it be a performance impact? And most importantly, am I missing anything here?