使用linq查询将列与列进行比较

时间:2012-07-24 07:04:54

标签: c# linq linq-to-entities

我遇到了一个我目前陷入困境的问题。例如:

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相比,并且在我尝试更改Emp_Start并停止到0430和2100时保持为0000和1000.

我在数据库中保存的时间类型是String而不是类型'time'。任何人都可以帮助我吗?我将展示我为此所做的任何代码。

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

2 个答案:

答案 0 :(得分: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.");
    }
}

答案 1 :(得分:0)

您应该在btnUpdate_Click方法中隐式更新shifthours对象的属性。