先来一张表
序号 | 类别属性 | 相同类别的连续个数 |
1 | A | 1 |
2 | B | 2 |
3 | B | 2 |
4 | C | 3 |
5 | C | 3 |
6 | C | 3 |
7 | D | 3 |
8 | D | 3 |
9 | D | 3 |
10 | E | 5 |
11 | E | 5 |
12 | E | 5 |
13 | E | 5 |
14 | E | 5 |
15 | F | 2 |
16 | F | 2 |
类别 | 起序号 | 止序号 | 个数 |
A | 1 | 1 | 1 |
B | 2 | 3 | 2 |
C | 4 | 6 | 3 |
D | 7 | 9 | 3 |
E | 10 | 14 | 5 |
F | 15 | 16 | 2 |
List<string> hebingList = new List<string>();
if(dataTable!=null && dataTable.Rows.Count>0)
{
int currentStartPosition=1;
int currentEndPosition = 1;
int currentSameRows = 1;
bool isInRows=false;
for (int i = 0; i < dataTable.Rows.Count; i++)
{
//处理行合并
int x = i + 1;
int sameRows = dataTable.Rows[i]["rowNumOfThisUnit"].ParseTo<int>(0);
if (sameRows > 1 && isInRows == false)
{
currentStartPosition = x;
isInRows = true;
currentEndPosition = x + sameRows-1;
currentSameRows = sameRows;
}
if(x>=currentEndPosition && isInRows==true)
{
hebingList.Add(currentStartPosition+","+currentEndPosition);
isInRows = false;
}
}
}
string strHeBing = JsonHelper.GetJson<List<string>>(hebingList);
将得到类似如下数据:
["1,3","7,8","9,15","17,18"]