I tried to find any solution on stackoverflow, but i didn't found it.
I have a DataTable, obtained from excel sheet. This is not clear table, it contain many sub-tables (starts with headers) and other necessary information above of them (which may contain empty rows). For example:
Line1 : Other data...
[empty line]
Line2: Other data...
[empty line]
...................
ColA | ColB | Type |
------------------------------
AAA | BBB | IN |
AAA | BBB | OUT |
AAA | BBB | IN |
Line1 : Other data...
[empty line]
Line2: Other data...
[empty line]
...................
ColA | ColB | Type |
------------------------------
AAA | BBB | IN |
AAA | BBB | OUT |
AAA | BBB | OUT |
I want to split the data table into multiple data tables that begin with many rows of necessary information, then the table itself and ends with empty row. As a result, I have to get DataTable[] data type.
I tried to obtain indexes of the last row of the sections of the data table (if DataRow contain "in" or "out" and next index contain empty row), but i don't know if is a good code and a good solution for further split a data table:
var indexes = dataTable.AsEnumerable()
.Where(x => x.ItemArray.All(rowCell => rowCell.ToString() == string.Empty))
.Where(
x => dataTable.Rows[dataTable.Rows.IndexOf(x) - 1].ItemArray.Any(
item => Regex.Match(
item.ToString(),
"^in|out$",
RegexOptions.IgnoreCase).Success))
.Select(x => dataTable.Rows.IndexOf(x)).ToArray();
I have two Where linq conditions for check whether exist empty row after row that contain "in" or "out" words.
How can I split the DataTable by these indexes? I want to find a similar Linq Expression for this purpose. As a result, I have to get the following tables:
Table #1
Line1 : Other data...
[empty line]
Line2: Other data...
[empty line]
...................
ColA | ColB | Type |
------------------------------
AAA | BBB | IN |
AAA | BBB | OUT |
AAA | BBB | IN |
Table #2
Line1 : Other data...
[empty line]
Line2: Other data...
[empty line]
...................
ColA | ColB | Type |
------------------------------
AAA | BBB | IN |
AAA | BBB | OUT |
AAA | BBB | OUT |
I know how to process this data further, but I don't know how to split the data table.