## 两个时间相减得到差值小时//通过Linq对DataTable进行Group By 原

_Somuns

`````` if (dt.Rows.Count > 0)
{
for (int i = 0; i < dt.Rows.Count; i++)
{
DateTime dtone;
DateTime dttwo;
DateTime dtthree;
DateTime dtfour;
TimeSpan span1;
string DiffStartHours;
string DiffEndHours;
dtone = Convert.ToDateTime(CommonFunction.MakeDateFormat(dt.Rows[i][1].ToString()));//Convert.ToDateTime("2015-8-8 06:00:00")
if (dt.Rows[i][3].ToString() == "")
{
DiffStartHours = "-";
}
else
{
dttwo = Convert.ToDateTime(CommonFunction.MakeDateFormat(dt.Rows[i][3].ToString()));
span1 = dtone.Subtract(dttwo); //算法是dtone 减去 dtwo
DiffStartHours = (span1.Days * 24 + span1.Hours).ToString();
}

dtthree = Convert.ToDateTime(CommonFunction.MakeDateFormat(dt.Rows[i][2].ToString()));//Convert.ToDateTime("2015-8-8 06:00:00")
if (dt.Rows[i][3].ToString() == "")
{
DiffEndHours = "-";
}
else
{
dtfour = Convert.ToDateTime(CommonFunction.MakeDateFormat(dt.Rows[i][4].ToString()));
TimeSpan span2 = dtthree.Subtract(dtfour);
DiffEndHours = (span2.Days * 24 + span2.Hours).ToString();
}

//tss.Text = span.Days + "天" + span.Hours + "小时" + span.Minutes + "分钟" + span.Seconds + "秒" + span.TotalDays;
dtResult.Rows.Add(dt.Rows[i][0], dt.Rows[i][1], dt.Rows[i][2], dt.Rows[i][3], dt.Rows[i][4], DiffStartHours, DiffEndHours);
}
}``````

===================================

``````   private DataTable ConvertTable(DataTable dt)
{
DataTable dtResult = new DataTable();
DataTable dtResult2 = new DataTable();
if (dt != null && dt.Rows.Count>0)
{
for (int i = 0; i < dt.Rows.Count; i++)
{
DateTime dtone;
DateTime dttwo;
TimeSpan span1;
int DurationMinutes;
dtone = Convert.ToDateTime(CommonFunction.MakeDateFormat(dt.Rows[i][1].ToString()));//Convert.ToDateTime("2015-8-8 06:00:00")
if (dt.Rows[i][1].ToString() == ""|| dt.Rows[i][2].ToString() == "")
{
DurationMinutes = 0;
}
else
{
dttwo = Convert.ToDateTime(CommonFunction.MakeDateFormat(dt.Rows[i][2].ToString()));
span1 = dttwo.Subtract(dtone); //算法是dtwo 减去 dtone
DurationMinutes =( (span1.Days * 24 + span1.Hours)*60+span1.Minutes);
}
//tss.Text = span.Days + "天" + span.Hours + "小时" + span.Minutes + "分钟" + span.Seconds + "秒" + span.TotalDays;
}

dtResult2 = dtResult.Clone();//将dt克隆到dtResult,但是dtResult里面并没有数据，只有结构，这是COPY方法的区别
DataTable dtName = dtResult.DefaultView.ToTable(true, "EVENT_CODE");//选择某一列中不同项的名称，这里选的是C1列只有A和B两个不同的项
//接下通过循环分组求和
for (int i = 0; i < dtName.Rows.Count; i++)
{
DataRow[] rows = dtResult.Select("EVENT_CODE='" + dtName.Rows[i][0] + "'");//注意这里多一对单引号，在等号后面有一个单引号这点一定不能忘记，否则运行的时候会出错，提示找不到列
DataTable temp = dtResult2.Clone();
foreach (DataRow row in rows)
{
}
DataRow dr = dtResult2.NewRow();
dr[0] = dtName.Rows[i][0].ToString();
dr[1] = temp.Compute("sum(EVENT_DURATION_TIME)", "");//compute 是一个函数用于计算，只有两个参数
}
}

return dtResult2;
}``````

### _Somuns

2017/11/16
0
0
js两个时间戳比较

2017/10/22
0
0
sql中两个时间类型相减得到的值

2012/05/15
8.8K
0
C# LINQ学习（3个例子）

2014/08/13
132
0
Mysql日期函数

MySQL 为日期增加一个时间间隔：dateadd() set @dt = now(); select dateadd(@dt , interval 1 day); - 加1天 select dateadd(@dt , interval 1 hour); -加1小时 select dateadd(@dt , inter......

2016/02/26
121
0

Kettle自定义jar包供javascript使用

CREATE_17

102
0

javail

79
0

73
0

SongAlone

124
0

shzwork

81
0