What is important to remember when using LINQ is that it leads to a query, not the results of that query. groupsToAdd not a list of elements, it is just a query definition that some elements can receive when necessary.
groupsToAdd does not actually iterate the original sequence (which is groups1 ) or does predicate checking (which depends on the state of groups2 ) until it repeats.
You repeat groupsToAdd twice. Once with AddRange call and again with Dump call. The second time you repeat it, group2 has changed, and therefore the query results have also changed.
If you want to avoid this delayed execution, you can materialize the request immediately by changing the code this way:
groupsToAdd = groups1.Where(g => false == groups2.Contains(g)); .ToList();
This will evaluate the query at that point in time so that groupsToAdd present the query results instead of the query itself.
Servy source share