Nahum
Nahum

Reputation: 7197

DataTable Find Item

im trying to do something like this

 var names_en = dtTmp.AsEnumerable();
 var names = names_en.Where(a => a["ID"] == "1");
 string name = names["Name"].ToString();

My goal is to find the row where ID equalls 1 and keep the Name as string

I know I have ID collumn I know that I have a row where ID==1 I see it in the debugger. but names lets that enumeration yields no results.

also is there a better way to do this?

Upvotes: 0

Views: 134

Answers (2)

ducmami
ducmami

Reputation: 215

var names = names_en.Where(a => a["ID"] == "1"); 

return the List of result you must get the first record

Upvotes: 0

Habib
Habib

Reputation: 223392

Its better if you use DataRowExtension.Field method which is strongly typed. Also make sure you ID is of type string, otherwise you will get an exception. You may specify the type accordingly.

string name = dtTmp.AsEnumerable()
                .FirstOrDefault(r => r.Field<string>("ID") == "1")
                .Field<string>("Name");

Upvotes: 1

Related Questions