2012-08-31 14 views
2

我對編程相當陌生,但一直負責維護由以前的員工創建的一些應用程序。我有一個?:陳述,現在需要處理的不僅僅是一個真實或虛假的陳述,但我不知道如何去做。有問題的代碼是:轉換?:語句處理超過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個督導添加到列表中,但還沒有想出一個辦法做到這一點不使一切不愉快。我很抱歉,如果這是一個太簡單的問題,或者我遺漏了一些您可能需要知道的細節,正如我所說的,我對這一切都很陌生。

+0

什麼是它是否是SUP1的標準 - SUP6? –

+0

讓你的代碼更具可讀性將是一個好的開始。我將'loadEmp.Entities.Where(emp => emp.EmployeeID == _EmployeeID).First()'放入一個變量中,而不是一遍又一遍地調用它。 –

+0

主管依賴於什麼是ShiftID。 – user1639524

回答

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 operators + Throw if unmatched

我可能會代替去對於像KDiTraglia這樣的Method方法,您只需傳入EmpId並獲取主管名稱,或者像Alexei Levenkov所說的詞典查找方法。

+0

良好的可讀性。 – BlueM

+0

@BlueM - 我會爭辯說,即使作者認爲這是一個可怕的代碼。這爲問題的作者解決了問題,但它總體上是一個可怕的想法。 –

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"; 
} 
4

此代碼不必要地難以維護,而且效率低下且不太防守。代碼正在檢索員工三次。

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

上面一行(和其他人)會拋出一個異常,如果有_EmployeeID員工不存在。相反,如果您希望只有一個具有該ID的員工(應該是這種情況,因爲它看起來像該實體的主鍵),您可以使用FirstOrDefaultSingleOrDefault。如果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 
} 
+0

您是唯一真正建議作者修復其可怕代碼的人。 –

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 
       ... 
); 
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 
    } 
}