简体   繁体   English

比较数据网格视图列和列

[英]Comparing Data grid view column and columns

I'm stuck on one part whereby I have no idea how to solve it. 我只停留在其中一个我不知道如何解决它的部分。 Basically, I have one table, "Shifthours" and another one which is "employeeshift". 基本上,我有一个表“ Shifthours”和另一个表“ employeeshift”。 Shifthours table have shift_Start and shift_Stop. Shifthours表具有shift_Start和shift_Stop。 employeeshift table have StartTime and EndTime. employeeshift表具有StartTime和EndTime。 I'm comparing shift_Start and StartTime. 我正在比较shift_Start和StartTime。 I have linked this 2 tables together using foreign key and the question I asked is that I want the shift_Start to compare with the StartTime and shift_Stop to compare with the EndTime and see the employee fit which shift and the shift_Start and shift_Stop will appear at the column that the employee is eligible. 我已使用外键将这两个表链接在一起,我问的问题是我想让shift_Start与StartTime进行比较,而shift_Stop与EndTime进行比较,并查看适合员工的班次,并且shift_Start和shift_Stop将出现在列中该雇员有资格。

Currently I got a code that only joins 2 table together but not comparing the timings. 目前,我得到的代码仅将2个表连接在一起,而没有比较时间。

private void LoadAllEmpShift()
    {
        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;
            dgvShift.DataSource = BS;
        }
    }

Anyone knows how to do this? 有人知道该怎么做吗?

Edit: 编辑:

You said you were trying to find where the employee hours match with a set of shift times. 您说您正在尝试找出与一组轮班时间相匹配的员工时间。 It would be nice to have some sample data and the algorithm that you want to use to determine what is a good shift time match. 最好有一些样本数据和要用来确定什么是良好的换挡时间匹配的算法。

I have assumed here that the best way to do that is to base the employee's start time off the nearest shift start time. 我在这里假设最好的方法是根据最近的轮班开始时间来确定员工的开始时间。

In the following code, I use the let function to essentially look through the shift hours and find the set of shift hours that are nearest to the employee's start time. 在以下代码中,我使用let函数从本质上查看轮班时间,并找到最接近员工开始时间的轮班时间集。

var Viewemp = from ES in Setupctx.employeeshifts
      join sh in Setupctx.shifthours on ES.ShiftHourID equals sh.idShiftHours 
         into shifts
      let diff = shifts
          .OrderBy (s => 
                    // this is the line that needs attention:
                    System.Math.Abs((int)(ES.StartTime - s.shiftTiming_start))
                   )
                   .First ()
      select new
      {
          ES.EmployeeShiftID,
          ShiftHour_Start = diff.shiftTiming_start,
          ShiftHour_Stop = diff.shiftTiming_stop,
          ES.EmployeeName,
          ES.StartTime,
          ES.EndTime,
          ES.Date
      };

Update 更新资料

My type in database for the StartTime and EndTime is string instead of time 我在数据库中输入的StartTime和EndTime是字符串而不是时间

In the above code the important logic is finding the absolute value difference between ES.StartTime and s.shiftTiming_start and the smallest difference indicates the best match for shift hour. 在上面的代码中,重要的逻辑是找到ES.StartTimes.shiftTiming_start之间的绝对值差,并且最小的差表明班次小时的最佳匹配。 Unfortunately, your database stores this data as a string and you need to compare them as numeric. 不幸的是,您的数据库将此数据存储为字符串,因此您需要将它们作为数字进行比较。

Linq-to-Entities does not contain an easy way to convert string to int function. Linq-to-Entities不包含将string转换为int函数的简单方法。

I think your next step would be to look into how you can convert those string values to int values. 我认为您的下一步将是研究如何将这些string值转换为int值。 Take a look at this question as I think it might help you out: 看一下这个问题,因为我认为它可以帮助您:

Convert String to Int in EF 4.0 在EF 4.0中将字符串转换为Int

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM