LINQ query to filter datatable using list and select only specific columns

Hello guys,

I have a datatable and I want filter this datatable based on list of values and get only mentioned columnsin the output along with one conditional value in column.

so I am using this query by which I am able to filter datatable based on list of values but It doesnt It returns all column and cant assign conditional value

dt.AsEnumerable.Where(Function (x) arrRefList3.Contains(x(“Ref.”).ToString)).CopyToDataTable

so I am using other query but it returns no datat row

(From d In dt.AsEnumerable Where arrRefList3.Contains(d(“Ref.”).ToString)
Let sd = d(“Sales”).toString
Let pa = d(“Partner”).toString
Let sw = d(“Purchase No.”).toString.Trim
Let f = If(d(“Appointment”).ToString.ToLower.Contains(“sb”), “2”, “3” )
Let ra = New Object(){sd,sw,pa,f}
Select dt1.Rows.Add(ra)).CopyToDataTable
this is returning no rows

Maybe rewriting the filter part will help:

(From d In dt.AsEnumerable.Where(function (x) arrRefList3.Contains(x("Ref.").ToString))
Let sd = d("Sales").toString
Let pa = d("Partner").toString
Let sw = d("Purchase No.").toString.Trim
Let f = If(d("Appointment").ToString.ToLower.Contains("sb"), "2", "3" )
Let ra = New Object(){sd,sw,pa,f}
Select dt1.Rows.Add(ra)).CopyToDataTable

if it is not working please share some data samples with us. Thanks

Hi @ppr,

Its throwing object reference error.

Data can be found below.

Sales Partner Purchase No Your Ref. Appointment
10945 1642 6 92911 SOFTBOOK
5520 1805 3394469 92910

isnot LINQ statement related, when variables are not as expected / assumed


  • please check your post (image issue)
  • share also the details of arrRefList3 (e.g. values) with us

Thanks for support

Yes @ppr, its not linq query error I was just telling the output.

List —> 92910,92911,92912,92913,92914

Hey @Akshay_Suryawanshi ,

I see your issue.

Can you give a try to build a datatable with the template and try to run.

I got this working.
Here is screenshot for reference. please check it out and let me know if you still face issue.

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