ianbeks ianbeks - 1 year ago 139
C# Question

Linq : select value in a datatable column

How do you use LINQ (C#) to select the value in a particular column for a particular row in a datatable. The equivalent SQL would be:

select NAME from TABLE where ID = 0

Thanks in advance.

Answer Source
var name = from r in MyTable
            where r.ID == 0
            select r.Name;

If the row is unique then you could even just do:

var row = DataContext.MyTable.SingleOrDefault(r => r.ID == 0);
var name = row != null ? row.Name : String.Empty;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download