LINQ | Filter DataTable | Get Matched Row Index

Lets say a datatable with 4 columns col1, col2, col3, col4.

Need to filter this datatable (on col2 and col3) with 2 string values. If matched found, need to get the matched row index number.

I want to use Linq Query.

1 Like

Hey @Arup,

Here is the solution

DT_Test.Rows.IndexOf((DT_Test.AsEnumerable.Where(function(row) (row(“col2”).ToString.Equals(“String1”) or row(“col2”).ToString.Equals(“String2”)) and (row(“col3”).ToString.Equals(“String1”) or row(“col3”).ToString.Equals(“String2”))).First))

This will find the index of only the first match, if you want for every match then you can workaround by a loop or another nested LINQ.

Meanwhile, you can also even try Select statement to achieve the same.

Let me know for any queries.


can do it via index list
it returns -1 in case of it is not found

(From i In Enumerable.Range(0,dtData.Rows.Count)
Let r = dtData.Rows(i)
Where r(1).toString.Equals(“M#A”) And r(2).toString.Equals(“M#B”)
Select i).DefaultIfEmpty(-1).First

find starter help here:
FindRowIndex_FirstMatch_2Cols.xaml (6.9 KB)