基于其他变量列表更新变量

时间:2021-11-05 14:20:52

This is more of a logic question than implementation.

这更像是一个逻辑问题,而不是实现。

class Step {    
  public string StepStatus {get ; set;}

  public List<Task> Tasks {get ; set;}

  public Step() {

    Tasks= new List<Task>() ;
  }

  //on some event I call this method that updates StepStatus based on status of
  //all task statuses

  private void UpdateStepStatusFromTasks() {
    foreach (Task t in Tasks) {
            // t.TaskStatus
            // set StepStatus here  
            // LOGIC here

    }
  }
}

Each Task object t has its own TaskStatus string.

每个Task对象都有自己的TaskStatus字符串。

  1. if All Task Statuses are "DONE" , set Step Status to "DONE"

    如果所有任务状态均为“完成”,请将“步骤状态”设置为“完成”

  2. if All Task Statuses are "TODO" , set Step Status to "TODO"

    如果所有任务状态均为“TODO”,则将“步骤状态”设置为“TODO”

  3. if one of the Task Statuses are "DOING" , set Step Status to "DOING"

    如果其中一个任务状态为“DOING”,则将Step Status设置为“DOING”

  4. if one of the Task Statuses are "DONE" and one is "TODO" , set Step Status to "DOING"

    如果其中一个任务状态为“DONE”且一个为“TODO”,则将Step Status设置为“DOING”

What is the best way to implement this? In terms of code complexity and less kludgyness.

实现这个的最佳方法是什么?在代码复杂性和较少的kludgyness方面。

How I solved it :

我是如何解决的:

 private void UpdateStepStatusFromTasks()
        {



            if ( Tasks.All(t => t.TaskStatus.Equals("Not Started")))
                StepStatus = "Not Started";
            else if (Tasks.All(t => t.TaskStatus.Equals("Completed")))
                StepStatus = "Completed";
            else
                StepStatus = "In Progress";




        }

3 个解决方案

#1


foreach(var step in steps)
{
    step.StepStatus =
        step.Tasks.All(x => x.Status == "DONE") ? "DONE" :
        step.Tasks.All(x => x.Status == "TODO") ? "TODO" :
        step.Tasks.Any(x => x.Status == "DOING") ? "DOING" :
        step.Tasks.Any(x => x.Status == "DONE ") && step.Tasks.Any(x => x.Status == "TODO") ? "DOING"
        : "UNKNOWN_STATUS";
}

#2


As Blorgbeard pointed out, this is the perfect use case for all and any. An example:

正如Blorgbeard指出的那样,这是所有人和任何人的完美用例。一个例子:

tasks.All( (t) => t.Status == Done);

t.Status == Done is just an example, it can be any boolean expression.

t.Status ==完成只是一个例子,它可以是任何布尔表达式。

#3


You could do something like this:

你可以这样做:

var taskStatuses = Tasks.Select(t => t.TaskStatus).Distinct().ToList();
if (taskStatuses.Count == 1) {
    // all tasks are the same status, so that's the step status
    return taskStatuses[0];
}
if (taskStatuses.Contains("DOING")) return "DOING";
if (taskStatuses.Contains("DONE") && taskStatuses.Contains("TODO")) {
    return "DOING";
}
// whatever other logic you want

#1


foreach(var step in steps)
{
    step.StepStatus =
        step.Tasks.All(x => x.Status == "DONE") ? "DONE" :
        step.Tasks.All(x => x.Status == "TODO") ? "TODO" :
        step.Tasks.Any(x => x.Status == "DOING") ? "DOING" :
        step.Tasks.Any(x => x.Status == "DONE ") && step.Tasks.Any(x => x.Status == "TODO") ? "DOING"
        : "UNKNOWN_STATUS";
}

#2


As Blorgbeard pointed out, this is the perfect use case for all and any. An example:

正如Blorgbeard指出的那样,这是所有人和任何人的完美用例。一个例子:

tasks.All( (t) => t.Status == Done);

t.Status == Done is just an example, it can be any boolean expression.

t.Status ==完成只是一个例子,它可以是任何布尔表达式。

#3


You could do something like this:

你可以这样做:

var taskStatuses = Tasks.Select(t => t.TaskStatus).Distinct().ToList();
if (taskStatuses.Count == 1) {
    // all tasks are the same status, so that's the step status
    return taskStatuses[0];
}
if (taskStatuses.Contains("DOING")) return "DOING";
if (taskStatuses.Contains("DONE") && taskStatuses.Contains("TODO")) {
    return "DOING";
}
// whatever other logic you want