Hi Team,
This is my input where i need to extract the data from the data row which contains"1234789" to the row containing “Total” which was highlighted in the below image.
We need the solution in the form of linq query, like we would first search the combination of “1234789” from the first column like if the first column contains string “1234789” then from there we will insert in a new datatable and till next “total” appears, it will take the data in data table, i.e. once the first column contains “total”, it will stop there.
I am attaching the input excel file below: Book1.xlsx (9.4 KB)
Hi @yash.choursia ,
This LINQ query uses a startAdding flag to determine when to start adding rows to the result DataTable. The flag is set to true when a row with “1234789” in the first column is encountered. The loop continues until a row with “Total” in the first column is encountered, at which, the loop breaks.
DataTable sourceData = // your source data
DataTable resultData = new DataTable();
// Create columns in the result DataTable based on the columns of the source DataTable
foreach (DataColumn column in sourceData.Columns)
{
resultData.Columns.Add(column.ColumnName, column.DataType);
}
bool startAdding = false;
// Use LINQ to extract the data
var extractedData = from row in sourceData.AsEnumerable()
where startAdding || row.Field<string>(0) == "1234789"
select row;
// Add the extracted data to the result DataTable
foreach (DataRow extractedRow in extractedData)
{
if (extractedRow.Field<string>(0) == "1234789")
{
startAdding = true;
}
resultData.ImportRow(extractedRow);
if (extractedRow.Field<string>(0) == "Total")
{
break;
}
}