C#DataTable Linq按多列总和int和时间跨度分组

时间:2018-07-18 06:47:34

标签: c# linq lambda datatable linq-to-entities

我想将来自DataTable别名的Group BY之后的所有Volume&TotalTimes列求和为Temp_DT。请记住,“ Volume&All TotalTimes”列可能具有空值或空白值。 谁能帮助我通过 Linq方法

获取代码

样品Temp_DT:

enter image description here

结果应采用这种格式 enter image description here

我尝试了以下代码,但是Temp_DT结果为Null。

var Temp_DT = tempdt.AsEnumerable().Select(x =>
                    new
                    {
                        UID = x.Field<string>("UID"),
                        EMPNAME = x.Field<string>("Emp Name"),
                        EMPROLE = x.Field<string>("Role"),
                        SUPID = x.Field<string>("Sup ID"),
                        SUPNAME = x.Field<string>("Sup Name"),
                        DESIGNATION = x.Field<string>("Designation"),
                        VOLUME = x.Field<int>("Volume"),
                        LOGINTIME = x.Field<TimeSpan>("Login Time"),
                        BREAKTIME = x.Field<TimeSpan>("Break Time"),
                        HANDLETIME = x.Field<TimeSpan>("Handle Time"),
                        ACTIVETIME = x.Field<TimeSpan>("Active Time"),
                        HOLDTIME = x.Field<TimeSpan>("Hold Time"),
                        ACWTIME = x.Field<TimeSpan>("ACW"),
                        IDLETIME = x.Field<TimeSpan>("Idle"),
                        PRODUCTIVE = x.Field<TimeSpan>("Productive"),
                        NONPRODUCTIVE = x.Field<TimeSpan>("Non Productive"),
                        USERERROR = x.Field<TimeSpan>("User Error Time")
                    }).GroupBy(s => new { s.UID, s.EMPNAME, s.EMPROLE, s.SUPID, s.SUPNAME, s.DESIGNATION })
                    .Select(g => new
                    {
                        g.Key.UID,
                        g.Key.EMPNAME,
                        g.Key.EMPROLE,
                        g.Key.SUPID,
                        g.Key.SUPNAME,
                        g.Key.DESIGNATION,
                        VOLUME = g.Sum(x => Convert.ToInt16(x.VOLUME)),
                        LOGINTIME = new TimeSpan(g.Sum(x => x.LOGINTIME.Ticks)),
                        BREAKTIME = new TimeSpan(g.Sum(x => x.BREAKTIME.Ticks)),
                        HANDLETIME = new TimeSpan(g.Sum(x => x.HANDLETIME.Ticks)),
                        ACTIVETIME = new TimeSpan(g.Sum(x => x.ACTIVETIME.Ticks)),
                        HOLDTIME = new TimeSpan(g.Sum(x => x.HOLDTIME.Ticks)),
                        ACWTIME = new TimeSpan(g.Sum(x => x.ACWTIME.Ticks)),
                        IDLETIME = new TimeSpan(g.Sum(x => x.IDLETIME.Ticks)),
                        PRODUCTIVE = new TimeSpan(g.Sum(x => x.PRODUCTIVE.Ticks)),
                        NONPRODUCTIVE = new TimeSpan(g.Sum(x => x.NONPRODUCTIVE.Ticks)),
                        USERERROR = new TimeSpan(g.Sum(x => x.USERERROR.Ticks)),
                    });

1 个答案:

答案 0 :(得分:1)

您必须使用TimeSpan来解析SumTicks,如下所示。

var Temp_DT = tempdt.AsEnumerable().Select(x =>
                    new
                    {
                        UID = x["UID"],
                        EMPNAME = x["Emp Name"],
                        EMPROLE = x["Role"],
                        SUPID = x["Sup ID"],
                        SUPNAME = x["Sup Name"],
                        DESIGNATION = x["Designation"],
                        VOLUME = x["Volume"],
                        ERRORTIME = x["Error_Time"],                            
                        ACWTIME = x["ACW"],
                        BREAKTIME = x["Break Time"],
                        IDLETIME = x["Idle"],
                        NONPRODUCTIVE = x["Non Productive"],
                    }).GroupBy(s => new { s.UID, s.EMPNAME, s.EMPROLE, s.SUPID, s.SUPNAME, s.DESIGNATION })
                    .Select(g => {
                      var grouped = g.ToList();
                      return new
                      {
                          UID = g.Key.UID,
                          EMPNAME = g.Key.EMPNAME,
                          EMPROLE = g.Key.EMPROLE,
                          SUPID = g.Key.SUPID,
                          SUPNAME = g.Key.SUPNAME,
                          DESIGNATION = g.Key.DESIGNATION,
                          VOLUME = grouped.Sum(x => Convert.ToInt16(x.VOLUME)),
                          Error_Time = new TimeSpan(grouped.Select(x => ConvertTimeSpan(x.ERRORTIME.ToString())).ToList().Sum(r=> r.Ticks)),
                          ACW = new TimeSpan(grouped.Select(x => ConvertTimeSpan(x.ACWTIME.ToString())).ToList().Sum(r=> r.Ticks)),
                          Break = new TimeSpan(grouped.Select(x => ConvertTimeSpan(x.BREAKTIME.ToString())).ToList().Sum(r=> r.Ticks)),
                          Idle = new TimeSpan(grouped.Select(x => ConvertTimeSpan(x.IDLETIME.ToString())).ToList().Sum(r=> r.Ticks)),
                          NonProductive = new TimeSpan(grouped.Select(x => ConvertTimeSpan(x.NONPRODUCTIVE.ToString())).ToList().Sum(r=> r.Ticks))
                      };
                    }).ToList();

转换方法是

private static TimeSpan ConvertTimeSpan(string str)
{
   TimeSpan outputValue; 
   TimeSpan.TryParse(str, out outputValue);

   return outputValue;
}

C# Fiddle和示例数据。