Finding array of row indexes in data table based on one value?

i am currenlty using 'Lookup Data Table to get the row index based off a value, 1. but if the value occurs more than once how do i get an array of row indexes?
my dt look like:

head1 head2 head3
1 V1 V3
1 V2 V2
2 v1 v1

so i want my output as arrIndex = {0,1}

Hi. You can use Linq for this. :slight_smile:

arrIndex = dt.AsEnumerable().Where(Function(x) x("head1").ToString().Equals(yourValue)).Select(Function(x,idx) idx).ToArray()

when i do it i get this error.
do need to install i import from the bottom panel?
if so which one would i need?

Thank you

Hi! create one variable for it with the type of array[string] then assign it to the string.

i have done that but stil same error

On second thoughts, my initial solution will not have the desired results.
This should be fine:

dt.AsEnumerable.
Select(Function(rw, idx) New With {rw, idx}).
Where(Function(x) x.rw("head1").ToString.Equals(yourValue)).
Select(Function(x) x.idx).
ToArray()

For the AsEnumerable error, have a look here: Compile Error: "AsEnumerable" is not a member of "System.Data.DataTable" - #2 by supermanPunch

@Rowley101

as an altrnate:

arrIndexFound =

(From i in Enumerable.Range(0, yourDataTableVar.Rows.Count)
Where yourDataTableVar.Rows(i)("head1").ToString.Equals(yourValue)).
Select x = i ).ToArray()
1 Like

this works perfectly. thank you very much

That’s a really cool one! :clap:
I’ve also translated it to Method Syntax (as I find it easier to work with :smiley: ):

Enumerable.Range(0, dt.Rows.Count).
Where(Function(x) dt.Rows(x)("head1").ToString.Equals(yourValue)).
ToArray()

Glad it helped! :slight_smile:

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