Tuesday, November 15, 2011

refactoring nested foreach loops to LINQ expressions

foreach(Guid selectedProgramId in SelectedProgramIds)

{

   foreach(Program program in AvailablePrograms)

   {

      if (program.Id == selectedProgramId)

      {

         programsAlreadySelected.Add(program);

      }

   }

}

 
 

The above may be refactored to the following with one click using Resharper:

programsAlreadySelected.AddRange(from selectedProgramId in SelectedProgramIds from program in AvailablePrograms where program.Id == selectedProgramId select program);

No comments:

Post a Comment