使用linq

时间:2017-01-10 17:42:33

标签: c#-4.0 linq-to-sql linq-to-entities

我有以下方法返回一个列表。这非常有效。 我想要实现的细节可以在下面找到。

        public List<Employeehourstask> GetAllTaskForEmployee(long uniqueId, string empId, DateTime strDate)
                {
                    var decuniqueid = Convert.ToDecimal(uniqueId);
                    using (var objEmpTask = new FtCollectionEntities())
                    {
                        List<Employeehourstask> objTaskList = (from bj in objEmpTask.DATA_TASK_EMP.Where(
                                                                  s => s.DEPT_UNIQUE_ID == decuniqueid 
                                                                  && s.DAY_DATE.Value.Year == strDate.Year
                                                                  && s.DAY_DATE.Value.Month == strDate.Month
                                                                  && s.DAY_DATE.Value.Day == strDate.Day
                                                                  && s.EMPLID == empId)

                                    select new Employeehourstask()
                                    {
                                        Id = bj.UNIQUE_ID,
                                        Hours = bj.PRODUCTION_HOURS_ENTERED,
                                        MyCount = bj.PIECE_COUNT,
                                        Task = bj.TASK_CODE
                                    }).ToList();                                                                                                                                                                                                                         
                        return objTaskList;
                    }
                }

enter image description here

1 个答案:

答案 0 :(得分:0)

Task分组,然后Sum分组Hours / MyCount

List<Employeehourstask> objTaskList =   (
    from bj in objEmpTask.DATA_TASK_EMP.Where(s => 
        s.DEPT_UNIQUE_ID == decuniqueid && 
        s.DAY_DATE.Value.Year == strDate.Year && 
        s.DAY_DATE.Value.Month == strDate.Month && 
        s.DAY_DATE.Value.Day == strDate.Day && 
        s.EMPLID == empId)
    group bj by bj.TASK_CODE into bjg
    select new Employeehourstask() 
    {
        //Id = bjg.Select(bg => bg.UNIQUE_ID).FirstOrDefault(),
        Hours = bjg.Sum(bj => bj.PRODUCTION_HOURS_ENTERED),
        MyCount = bjg.Sum(bj => bj.PIECE_COUNT),
        Task = bj.Key
    }
).ToList();

您可以删除Id属性,因为您正在根据任务

聚合结果