Reputation: 13785
I want to get the value for Column B in the first row I find with Column A = "123".
I tried:
dataTable.AsEnumerable().Select(row => row["value"].ToString() == "123").ToString();
But I'm getting Expression cannot contain lambda expressions.
Any help would be appreciated, thanks.
EDIT:
I'm basically looking for the SQL equivalent of:
Select ColumnB
From MyTable
Where ColumnA = '123'
For the datatable using a lambda expression.
Upvotes: 0
Views: 676
Reputation: 53603
Take a look at the FirstOrDefault
method:
var foundRow = dataTable.AsEnumerable().FirstOrDefault(row => string.Equals (row["ColumnA"].ToString(), "123", StringComparison.OrdinalIgnoreCase ));
if (foundRow != null) {
// We found a match, do something with it.
var colBValue = foundRow["ColumnB"].ToString();
}
FirstOrDefault
returns the first element that matches a specified criteria, which in this case looks for the string "123" in the [ColumnA] column in your DataTable. If no element matches the criteria, FirstOrDefault
returns the object's default value which for DataTable is null
.
I don't know why you are getting the Exception you show, I didn't when I tried to reproduce it.
Upvotes: 1
Reputation: 1090
I believe you're looking for the Where
method
dataTable.AsEnumerable().Select(row=>row["Value"].ToString()).Where(row => row["value"].ToString() == "123");
Upvotes: 0