Chris Chris - 1 year ago 68
C# Question

C# Linq get values from column B if column A matches

Based on the table below, I'd like to use Linq to get all of the device names associated with Test ID

and store them in a list, is this possible? Assuming that is not always known which Test ID will be listed more than once.

Test ID DeviceName
1a dev1
1b dev2
1a dev2
1c dev1

Answer Source

If all you want to do is read all the values from column B then the following query is all you should need:

string testID;
// set testID from somewhere (user input, config file, another query etc.)

var results = dataTable.Where(t => t.Id == testID).Select(t => t.DeviceName).ToList();

results will be of type List<string>.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download