For comment below, here's some setup code:
var allItems = new List<Item>();
allItems.Add(new Item{Id = 1});//etc etc
allItems.Add(new Item{Id = 2, ParentId = 1});//etc etc
Given the setup code; the following code:
foreach (var parentItem in parentItems)
{
var childItems = allItems.Where(c => c.ParentId == parentItem.Id);
...
}
Displays a warning under parentItem.Id:
"Access to foreach variable in closure. May have different behavior when compiled with different versions of compiler."
But if I convert the above code (using ReSsharper) to the following query expression:
from parentItem in parentItems
let childItems = allItems.Where(c => c.ParentId == parentItem.Id)
...
I don't get the warning.
Does this syntax (seemingly equal to the foreach syntax) not have the same closure warning/problem?
Or is ReSharper not picking up this problem in the query expression syntax?