say I want to iterate over a list of IDs and use them to compare some properties in a QueryOver-statement as in:
foreach(int id in myIdList)
{
QueryOver<Tabe> query = QueryOver.Of<Table>()
.Where(t => t.SomeForeignKey == id);
}
Now my compiler advised me to create an extra variable for the iterator id, as it will otherwise be treated as a closure accessing a foreach-variable. So to be on the safe side I adjustet the code to:
foreach(int id in myIdList)
{
int id1 = id;
QueryOver<Tabe> query = QueryOver.Of<Table>()
.Where(t => t.SomeForeignKey == id1);
}
And the compiler stopped complaining.
For the first version the compiler said, that the behaviour of the statement differs depending on the compiler version, so I am wondering in which cases it would be save to use the first expression and why it produces correct / incorrect results. And why is it in the end better to use the second version?
Thank you for any insights!