Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Datatable select with multiple conditions

I have a datatable with 4 columns A, B, C and D such that a particular combination of values for column A, B and C is unique in the datatable.

Objective: To find the value of column D, for a given combination of values for column A, B and C.

I guess looping over the set of data rows should do it. Is there a way to use Datatable.Select() to accomplish this? To be more specific - can I have multiple conditions in the select filter i.e. a logical AND operator connecting conditions for each of the columns A, B and C.

like image 624
Arnkrishn Avatar asked Jan 02 '10 08:01

Arnkrishn


People also ask

How check multiple conditions in DataTable select?

DataRow[] results = table. Select("A = 'foo' AND B = 'bar' AND C = 'baz'"); See DataColumn. Expression in MSDN for the syntax supported by DataTable's Select method.

What is DataRow C#?

A DataRow represent a row of data in data table. You add data to the data table using DataRow object. A DataRowCollection object represents a collection of data rows of a data table.


1 Answers

Yes, the DataTable.Select method supports boolean operators in the same way that you would use them in a "real" SQL statement:

DataRow[] results = table.Select("A = 'foo' AND B = 'bar' AND C = 'baz'"); 

See DataColumn.Expression in MSDN for the syntax supported by DataTable's Select method.

like image 117
Michael Petrotta Avatar answered Sep 20 '22 18:09

Michael Petrotta