Linq to select column A based of condition in columnB

Hi all.

I have a datatable - dt with multiple columns.
I need to extract column A values with condition on columnB.

If and only column B is Yes, then only column A values only must be extracted and can be stored in an array of strings.

any ideas?

Thanks in advance!!

Hi @Ray_Sha1 ,

Have you tried using Filter Datatable Activity ?

1 Like

Yup!
I need the linq approach for this one.

@Ray_Sha1 ,

You could Check the Below Expression :

DT.AsEnumerable.Where(Function(x)x("ColumnB").ToString.Equals("Yes")).Select(Function(x)x("ColumnA").ToString).ToArray
2 Likes

Hi,

Try This
DT_Input.AsEnumerable().Where(Function(DR) DR(“Column2”).ToString.Equals(“Yes”)).CopyToDataTable().AsEnumerable().Select(Function(c1) c1(“Column1”).ToString).ToArray()

Sample xaml is attached
Linq.xaml (7.2 KB)

Thanks,
Muthuraj

1 Like

Hi @Ray_Sha1 ,

plz check below

(From row In (dtTest.Select(“[B]=‘Yes’”).CopyToDataTable).AsEnumerable() Select Convert.Tostring(row(“A”))).ToList()

InPut dt:

A B
1 No
2 Yes
3 Yes
4 No
5 Yes
6 No
7 No
8 No
9 Yes
10 Yes

Sample.xaml (6.3 KB)

Thanks,
RajKumar

1 Like

Hi @Ray_Sha1 ,

kindly find exception handled Linq

If(dtTest.Select(“[B]=‘Yes’”).Count>0,((From row In (dtTest.Select(“[B]=‘Yes’”).CopyToDataTable).AsEnumerable() Select Convert.Tostring(row(“A”))).ToList()),Nothing)

Thanks,
RajKumar

1 Like

All the above solutions work fine

2 Likes

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