NeonRant
4/27/2020 - 11:43 PM

Multiple statements in a LINQ ForeEAch

Adding multiple statements in a LINQ ForEach by using parenthesis arond them.

// The old way

foreach (var tour in operatorsTours)
            {
                if (tour != null)
                {
                    tour.SetValue("IsTourUnderPlan", false);
                    tour.Update();
                }
            }
            
// But because it has two statements you cannot do this with LINQ
operatorsTours.ForEach(t => t.SetValue("IsTourUnderPlan", false); t.Update(););

// You must use parenthisis around the statements

operatorsTours.ForEach(t => 
                          { 
                            t.SetValue("IsTourUnderPlan", false); 
                            t.Update(); 
                          }
                        );

// And you can put it all on a single line
operatorsTours.ForEach(t => { t.SetValue("IsTourUnderPlan", false); t.Update(); });