DataTable.Select Method (String, String)


The .NET API Reference documentation has a new home. Visit the .NET API Browser on to see the new experience.

Gets an array of all DataRow objects that match the filter criteria, in the specified sort order.

Namespace:   System.Data
Assembly:  System.Data (in System.Data.dll)

public DataRow[] Select(
	string filterExpression,
	string sort


Type: System.String

The criteria to use to filter the rows. For examples on how to filter rows, see DataView RowFilter Syntax [C#].

Type: System.String

A string specifying the column and sort direction.

Return Value

Type: System.Data.DataRow[]

An array of DataRow objects matching the filter expression.

To form the filterExpression argument, use the same rules for creating the DataColumn class's Expression property value. The Sort argument also uses the same rules for creating class's Expression strings.

If the column on the filter contains a null value, it will not be part of the result.

The following example uses a filter expression to return an array of DataRow objects.

using System;
using System.Data;

public class A {  

   public static void Main() {      
      DataTable table = new DataTable("Orders");
      table.Columns.Add("OrderID", typeof(Int32));
      table.Columns.Add("OrderQuantity", typeof(Int32));
      table.Columns.Add("CompanyName", typeof(string));      
      table.Columns.Add("Date", typeof(DateTime));

      DataRow newRow = table.NewRow();
      newRow["OrderID"] = 1;
      newRow["OrderQuantity"] = 3;
      newRow["CompanyName"] = "NewCompanyName";
      newRow["Date"] = "1979, 1, 31";

      // Add the row to the rows collection.

      DataRow newRow2 = table.NewRow();
      newRow2["OrderID"] = 2;
      newRow2["OrderQuantity"] = 2;
      newRow2["CompanyName"] = "NewCompanyName1";

      DataRow newRow3 = table.NewRow();
      newRow3["OrderID"] = 3;
      newRow3["OrderQuantity"] = 2;
      newRow3["CompanyName"] = "NewCompanyName2";

      // Presuming the DataTable has a column named Date.
      string expression = "Date = '1/31/1979' or OrderID = 2";
      // string expression = "OrderQuantity = 2 and OrderID = 2";

      // Sort descending by column named CompanyName.
      string sortOrder = "CompanyName ASC";
      DataRow[] foundRows;

      // Use the Select method to find all rows matching the filter.
      foundRows = table.Select(expression, sortOrder);

      // Print column 0 of each returned row.
      for (int i = 0; i < foundRows.Length; i++)

.NET Framework
Available since 1.1
Return to top