Linq Query for extracting excel from row containing specific string till row containing specific string

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.

Here we can see that the input is in the above format and the output we need is attached below.
Expected output below:

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;
    }
}

@yash.choursia Check this below workflow attached,
Uipath_Extract_GetRangeOfRows.xaml (12.3 KB)

Hope this may help you :slight_smile:

3 Likes

Thanks Manish, it worked.

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.