How to select rows from a data table using Linq but with reference to an array

I have a data table that has some client ids, names, and countries, I have another array that has the client ids I require for the process ( example: id={1011,1015}). Is there any chance where we can directly get the rows where ClientID is the same as array element?


Here is the example excel file.

testData.xlsx (8.4 KB)

Thanks in Advance

Hi @Sadmim_Hossain1,

Give a try on this

(From d1 In dt1.AsEnumerable()
From d2 In ArrayOfStrings.AsEnumerable()
Where d1(0).toString.Equals(d2(0).toString.Trim)
Let var = New Object(){d1(0), d2(0)}
Select r = dtResult.Rows.Add(var)).CopyToDataTable


can be understood as a filter task

(From d in dtData.AsEnumerable
Where  arrIDs.Any(Function (x) x.toString.Equals(d("ClientID").toString.Trim))
Select r = d).CopyToDataTable

Hi @Sadmim_Hossain1 ,

You could do it like so as well if you are using an Array of Integers->

dt_sampleData.AsEnumerable().Where(Function(w) arr_ids.Select(Function(s) s.ToString).Contains(w("ClientID").ToString)).CopyToDataTable()

GetRowsBasedOnArray.xaml (7.5 KB)

Kind Regards,
Ashwin A.K

1 Like

This worked perfectly.

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