Unable to add all values from datatable to list based on condition

Hi I’m trying to add values from column b from a datatable if the value in column a matches a specific value. I’ve used the below LINQ to filter and add to a list variable but only the first value of column b is added. Have tested with different values of column A, and ensure each distinct value in column a has two or more entries of different value in column b. Any and all advice greatly appreciated.

(From row In dataTable.AsEnumerable()
Where row.Field(Of String)(columnAName) Is specificValue
Select row.Field(Of String)(columnBName)).ToList()

Hi @QTH

Please check with the below linq query:

dt.AsEnumerable().Where(Function(x) x("ColumnName").ToString().Equals("Condition")).Select(Function(x) x("ColumnName")).ToList()

Regards

Hi @vrdabberu

Thanks for the prompt response. Your LINQ query returns an object instead of string. May I know which part of the query I could amend to change the return to be of string so it could fit into my list?

Thank you.

Hi @QTH

Please check now

dt.AsEnumerable().Where(Function(x) x("ColumnName").ToString().Equals("Condition")).Select(Function(x) x("ColumnName").ToString).ToList()

Regards

Thank you so much for your assistance!

1 Like

You’re welcome @QTH

Happy Automation!!

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