LINQ and DataTable

//Select entire row (or List of rows)
var results = (from DataRow row in dt.Rows
               where (String)row["ColulmnA"] == "SearchCriteria"
               select row).FirstOrDefault(); //or .ToList()

var test = results["ColulmnB"];    //if .FirstOrDefault() used.
var test = results[0]["ColulmnB"]; //if .ToList() used.

//Select specific column (or, List of columns)
var query = (from row in dt.AsEnumerable()
             where row.Field<int>("ColumnA") == 123
             select row.Field<int>("ColumnB")).FirstOrDefault(); //or .ToList()
var test = query;    //if .FirstOrDefault() used.
var test = query[0];    //if .ToList() used.
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s