循环数据通道

时间:2014-11-04 07:18:49

标签: c# datatable

我有一个数据表,如:

 DataTable dtAbsent = new DataTable();
 dtAbsent.Columns.Add("Date", typeof(DateTime));
 dtAbsent.Columns.Add("TypeOfLeave", typeof(string));
 dtAbsent.Columns.Add("Month", typeof(string));

我正在向它添加数据:

dtAbsent.Rows.Add("2014-10-05', "H","Current");

所以我的最终数据表是:

     Date    |   TypeOfLeave  | Month
11/10/2014 12:00:00 AM  L   Current
11/9/2014 12:00:00 AM   H   Current
11/8/2014 12:00:00 AM   H   Current
11/7/2014 12:00:00 AM   L   Current
11/6/2014 12:00:00 AM   H   Current
11/5/2014 12:00:00 AM   H   Current
11/4/2014 12:00:00 AM   L   Current

我想循环记录并在连续的TypeOfLeave =“L”之间得到TypeofLeaves =“H”的数字

如何设计循环以便为TypeOfLeave =“L”获取连续的行?或者,如果不使用循环来获取下一条记录,还有更好的方法吗?

更新

需要输出:

First  TypeofLeave ='L' - 11/10/2014 12:00:00 AM    L   Current
Next TypeOfleave='L' - 11/7/2014 12:00:00 AM    L   Current

So no of typeOfleave ='H' in between First and next =2

then,
first TypeOfleave='L' - 11/7/2014 12:00:00 AM   L   Current
next TypeOfleave='L' 11/4/2014 12:00:00 AM  L   Current

and no of typeOfleave ='H' in between First and next =2

1 个答案:

答案 0 :(得分:1)

试试这个:

DataRow[] result = dtAbsent.Select("TypeOfLeave = 'L'");

foreach (DataRow row in result) 
{   
   Console.WriteLine("{0}", row[0].ToString()); 
}