2

我对编程还很陌生,但我的任务是维护一些由以前的员工创建的应用程序。我有一个 ?: 声明,现在需要处理的不仅仅是一个真假声明,但我不知道如何去做。有问题的代码是:

    MailDomainContext mail = new MailDomainContext();
      mail.Load(mail.GetMailsQuery("Workforce Attendence Issue",
               loadEmp.Entities.Where(emp => emp.EmployeeID == _EmployeeID).First().Username,
               (loadEmp.Entities.Where(emp => emp.EmployeeID == _EmployeeID).First().EmployeeShiftID >= 2 ? "supervisor1" : "supervisor2"),
               loadEmp.Entities.Where(emp => emp.EmployeeID == _EmployeeID).First().FirstName,
               attendence.AttendenceDate.ToString("MM/dd/yyyy"),
               attendence.TimeLost,
               loadAbs.Entities.Where(abs => abs.AbsenceID == attendence.AbsenceID).First().AbsenceDescription,
               (from inf in loadAtt.Entities
               where inf.EmployeeID == _EmployeeID
               where inf.AttendenceDate > DateTime.Now.AddDays(30 * -1)
               where inf.Approved == false
               select inf).Count() + 1,
               attendence.UTOUsed
               ), null, null);

更具体地说,这一行:

    (loadEmp.Entities.Where(emp => emp.EmployeeID == _EmployeeID).First().EmployeeShiftID >= 2 ? "supervisor1" : "supervisor2"),

我需要在列表中再添加 4 名主管,但还没有找到一种不会让其他一切都不愉快的方法。如果这是一个太简单的问题,或者我遗漏了一些您可能需要知道的细节,我深表歉意,因为我说过我对这一切都很陌生。

4

5 回答 5

4

这段代码不必要地难以维护,而且效率低下而且不是很有防御性。该代码正在检索员工三次。

loadEmp.Entities.Where(emp => emp.EmployeeID == _EmployeeID).First().Username

_EmployeeID如果员工不存在,上述行(和其他行)将引发异常。相反,您可以使用FirstOrDefault,或者SingleOrDefault如果您希望只有一名员工具有该 ID(应该是这种情况,因为它看起来像该实体的主键)。如果loadEmp实际上是一个实体框架DbContext,那么您也可以使用Find.

您可以执行一次此查询并将结果存储在局部变量中。

var employee = loadEmp.Entities.SingleOrDefault(emp => emp.EmployeeID == _EmployeeID);

if (employee == null)
{
   // Handle employee not found
}

然后,要根据员工获取主管字符串,您可以创建一个方法,该方法采用计算主管字符串所需的最少信息量,并将其传递给方法以获取结果。

GetSupervisorRole(employee.EmployeeShiftID);

...

private string GetSupervisorRole(int employeeShiftID)
{
   // Logic here
}
于 2012-08-31T19:41:56.800 回答
3

一种方法是将该代码提取到一个方法中,然后以您想要的任何方式编写该方法。

另一种方法是使用字典将键(如果它们的数量很少)映射到值。

var id =3;
var mapping = new Dictionary<int, string>() { 
  { 1, "first" },
  { 2, "second" },
  { 3, "first" } //you can map 2 values (1,3) to the same "first" string
};

string value;
if (!mapping.TryGetValue(id, out value))
{
  value = "unknown";
}
于 2012-08-31T19:38:27.250 回答
2

我会替换整个部分

loadEmp.Entities.Where(emp => emp.EmployeeID == _EmployeeID).First().Username,
(loadEmp.Entities.Where(emp => emp.EmployeeID == _EmployeeID).First().EmployeeShiftID >= 2 ? "supervisor1" : "supervisor2"),
 loadEmp.Entities.Where(emp => emp.EmployeeID == _EmployeeID).First().FirstName,

//Assign these variables ahead of time so others reading your code can 
//figure out what's going on
var EmpID = loadEmp.Entities.Where(emp => emp.EmployeeID == _EmployeeID).First();
var UserName = EmpID.UserName;
var FirstName = EmpID.FirstName;
var Title = GetTitle(EmpID.EmployeeShiftID);

//Your original call
Mail.Load(mail.GetMailsQuery("Workforce Attendence Issue",
     UserName,
     Title,
     FirstName,
     //...Etc
);

//New method you will need to add, you could do this logic in line, but this will
//be less messy
private string GetTitle(int ShiftID)
{
    switch (ShiftID)
    {
         case 1:
             return "supervisor1";
             break;
         case 2:
             return "supervisor2";
             break;
         //...etc
    }
}
于 2012-08-31T19:45:11.030 回答
2

创建以下方法:

string GetSupervisor(int employeeShiftId) {
    if (employeeShiftId == 1) supervisor = "supervisor1";
    else if (employeeShiftId == 2) supervisor = "supervisor2";
    else if (employeeShiftId == 3) supervisor = "supervisor3";
    else if (employeeShiftId == 4) supervisor = "supervisor4";
}

然后从您的代码中调用它并将结果分配给一个变量supervisor,然后您可以使用它mail.Load()

int employeeShiftId = loadEmp.Entities
          .Where(emp => emp.EmployeeID == _EmployeeID).First()
          .EmployeeShiftID;
string supervisor = GetSupervisor(employeeShiftId);

MailDomainContext mail = new MailDomainContext();
mail.Load(mail.GetMailsQuery("Workforce Attendence Issue",
               loadEmp.Entities.Where(emp => emp.EmployeeID == _EmployeeID).First().Username,
               supervisor, // <-- Then use it here
               ...
);
于 2012-08-31T19:43:01.797 回答
1

并不是说这是一个好主意,但是您可以像这样组合内联:

    static void Main(string[] args)
    {
        int EmpId = 2;
        string supervisor = EmpId == 4 ? "Supervisor4" :
                            EmpId == 3 ? "Supervisor3" :
                            EmpId == 2 ? "supervisor2" :
                                         "supervisor1" ;
        Console.WriteLine(supervisor);

    }

您可以在另一个堆栈问题中看到另一个示例:Legible or not: C# multiple ternary operator + Throw if unmatched

我可能会改用 KDiTraglia 建议的 Method 方法,您只需传入 EmpId 并取回主管名称,或者像 Alexei Levenkov 提出的 Dictionary 查找方法。

于 2012-08-31T19:38:16.050 回答