How to update a global variable inside `where` clause in LINQ?
问题 I want to filter a list using LINQ with Where extension method. But apart from filtering I also want to update a global variable inside Where . However I cannot do it. Consider this example: var list = new List<string> { "1", "2", "3", "4", "5" }; bool flag = false; var newList = list.Where(item => { flag = true; return item == "2"; }); // Here I expect flag = true, but in fact it's false Console.Write(flag); As you can see I set flag = true , still the value flag == false after execution. It