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.
Take a look at the
FirstOrDefaultmethod:FirstOrDefaultreturns 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,FirstOrDefaultreturns the object’s default value which for DataTable isnull.I don’t know why you are getting the Exception you show, I didn’t when I tried to reproduce it.