How to get the above data of the value using linq query

Column1 column2 column3
a b c
b b a
c b d
a d c
b c d
c b a

Get the above data of the column2 “d” appears and skip the below data

Hi @Ranga_prasad ,

Could you provide the Expected output for the Input data provided?

Also explain a bit more as to what do you want to perform on that data.

Hi @Ranga_prasad,

First try to get the row index where the column2 has “d” in it.

yourDataTable.AsEnumerable().[Select](Function(row) row.Field(Of String)("Column2")).ToList().FindIndex(Function(col) col = "d")

Using this index you can use Linq’s Skip and Take function to get the rows before this index



Can you try the following expression?

dt = dt.AsEnumerable.Take(dt.AsEnumerable.Select(Function(r,i) if(r("Column2").ToString="d",i,-1)).Max()+1).CopyToDataTable()


Column1 column2 column3
a b c
b b a
c b d

in this way i need output.

dtData.asEnumerable.TakeWhile(Function (x) Not x(1).toString.Trim.ToUpper.Equals("D")).CopyToDataTable

in case of we would also expect empty results then use toList like:

Result | List(Of DataRow) =
dtData.asEnumerable.TakeWhile(Function (x) Not x(1).toString.Trim.ToUpper.Equals(“D”)).toList

and evaluate on the Result.Count if CopyToDataTable can be executed or not

Hi Sarathi ,

Your linq query is working fine,
but i have to skip all the data after “d” in column2.
can you help me out of this.
o/p: Column1 column2 column3
a b c
b b a
c b d
Thanks & regards,


You have to get the index using my first Linq Query into a variable named “intIndexIdentified” and use the second linq query to get the rows before the row which has “d” in column2.


1 Like

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