2012-07-24 165 views
0

我遇到了一個問題,目前我堅持。例如:使用linq查詢比較列與列

EmployeeShiftID |  ShiftTime_Start  |  hiftTime_Stop  |  Name  | Emp_Start | Emp_Stop 
       |(linked with foreign key)| (linked with FK) |    |   | 
    1   |  0000    |   1000   |  Ken  | 0000 | 1000 

這些數據顯示在數據網格視圖中,外鍵鏈接。 Shift開始和停止也匹配Emp_Start並停止。問題是,當我更新Emp_start並停止時,ShiftTime_Start和Stop不會與Emp_Start和Stop相比較,並且保持爲0000和1000,因爲我試圖更改Emp_Start並停止到0430和2100.

我在數據庫中保存的時間類型是字符串,而不是類型'時間'。任何人都可以幫助我嗎?我會顯示我爲它所做的任何代碼。

private void btnUpdate_Click(object sender, EventArgs e) 
{ 
    using (testEntities Setupctx = new testEntities()) 
    { 
     int ID = Int32.Parse(lblID.Text); 
     var ESquery = (from es in Setupctx.employeeshifts 
         where es.EmployeeShiftID == ID 
         select es).First(); 

     ESquery.StartTime = txtStart.Text; 
     ESquery.EndTime = txtStop.Text; 
     ESquery.Date = txtDate.Text; 
     Setupctx.SaveChanges(); 
     txtStart.Text = ""; 
     txtStop.Text = ""; 
     txtDate.Text = ""; 
     this.Edit_Employee_Shift_Load(null, EventArgs.Empty); 
     MessageBox.Show("Employee's Shift Has Been Updated."); 
    } 
} 


private void LoadAllEditEmpShift() 
{ 
    using (testEntities Setupctx = new testEntities()) 
    { 
     BindingSource BS = new BindingSource(); 
     var Viewemp = from ES in Setupctx.employeeshifts 
         join shifthour sh in Setupctx.shifthours on ES.ShiftHourID equals sh.idShiftHours 
         select new 
         { 
          ES.EmployeeShiftID, 
          ShiftHour_Start = sh.shiftTiming_start, 
          ShiftHour_Stop = sh.shiftTiming_stop, 
          ES.EmployeeName, 
          ES.StartTime, 
          ES.EndTime, 
          ES.Date 
         }; 

     BS.DataSource = Viewemp; 
     dgvEmpShift.DataSource = BS; 
    } 
} 

回答

1

我認爲它應該是這樣的:

private void btnUpdate_Click(object sender, EventArgs e) 
{ 
    using (testEntities Setupctx = new testEntities()) 
    { 
     int ID = Int32.Parse(lblID.Text); 
     var ESquery = (from es in Setupctx.employeeshifts 
         where es.EmployeeShiftID == ID 
         select es).First(); 
     var SHquery = (from sh in Setupctx.shifthours where sh.idShiftHours == ESQuery.ShiftHourID 
         select sh).First(); 

     ESquery.StartTime = txtStart.Text; 
     ESquery.EndTime = txtStop.Text; 
     ESquery.Date = txtDate.Text; 
     SHquery.shiftTiming_start = ESquery.StartTime; 
     SHquery.shiftTiming_stop = ESquery.EndTime; 
     Setupctx.SaveChanges(); 
     txtStart.Text = ""; 
     txtStop.Text = ""; 
     txtDate.Text = ""; 
     this.Edit_Employee_Shift_Load(null, EventArgs.Empty); 
     MessageBox.Show("Employee's Shift Has Been Updated."); 
    } 
} 
0

您應該在btnUpdate_Click方法中隱式更新shifthours對象的屬性。

+0

護理離開我一些例子嗎? – Philemon 2012-07-24 08:00:56