Linq query for datatable to filter data based on empty rows before and after data

Please find below egample The Filter Data is =Ragu and the output should be all the rows based on start of empty rows and end of empty rows based on the filter input Ragu

@ppr Bro any thoughts for you on this LINQ query

we would recommend to solve it by calculation of

  • row index of the match value
  • segment start /length

A single Linq block statement has the risk to result very quickyl into a over LINQing Blackbox

Divided it into a split approach (assumption for the findindex. only 1 Ragu)

Find index of Ragu (enahnce by to upper…):
dtData.AsEnumerable.toList.FindIndex(Function (x) x(0).toString.Trim.Equals(“Ragu”))

when outcome is not -1 (not found)
Parts before Ragu:
dtData.AsEnumerable.Reverse().Skip(RaguIndex + 1).TakeWhile(Function (x) …
take as long it is not null or empty

Parts after Ragu:
dtData.AsEnumerable.Skip(RaguIndex).TakeWhile(Function (x) …

then concat both and make a datatable when needed

As anlternate we would also recommend to check more the business case on what is needed how often the different index is needed. Maybe creating helper cols telling segment startindex, endindex could also be an option.

Then filter for Ragu - receive: Ragu | 6 | 8 |
So we slice with: dtData.asEnumerable.Skip(6).Take(8-6+1).CopyToDataTable

Awesome bro will check with the guidance provided by you and revert you :slight_smile: Thanks for your valuable time for the solution

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