LINQ option for shorter code

LINQ is rad! You can pass a lambda expression to the Count method of an IEnumerable and it’ll do the looping for you! For Count(), if you want an item to be counted, return true from the lambda. Since Toggle.value is a bool that’s true when checked, it ends up being a really simple line of code.

private void UpdateProgress()
{
  int completedTaskCount = this.taskList.Children().Count(t => (t as Toggle).value);
  int totalTaskCount = this.taskList.Children().Count();

  float progress = (totalTaskCount > 0) ? (float)completedTaskCount / totalTaskCount : 0f;
  this.tasksProgressBar.value = progress;
  this.tasksProgressBar.title = $"Completed: {progress:P1}";
}
1 Like

Awesome job! :clap:

Privacy & Terms