DataTable.Select Method

Definition

Gets an array of DataRow objects.

Overloads

Select()

Gets an array of all DataRow objects.

Select(String)

Gets an array of all DataRow objects that match the filter criteria.

Select(String, String)

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

Select(String, String, DataViewRowState)

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

Select()

Source:
DataTable.cs
Source:
DataTable.cs
Source:
DataTable.cs

Gets an array of all DataRow objects.

C#
public System.Data.DataRow[] Select();

Returns

An array of DataRow objects.

Examples

The following example returns an array of DataRow objects through the Select method.

C#
private void GetRows()
{
    // Get the DataTable of a DataSet.
    DataTable table = DataSet1.Tables["Suppliers"];
    DataRow[] rows = table.Select();

    // Print the value one column of each DataRow.
    for(int i = 0; i < rows.Length ; i++)
    {
        Console.WriteLine(rows[i]["CompanyName"]);
    }
}

Remarks

To ensure the proper sort order, specify sort criteria with Select(String, String) or Select(String, String, DataViewRowState).

See also

Applies to

.NET 9 and other versions
Product Versions
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

Select(String)

Source:
DataTable.cs
Source:
DataTable.cs
Source:
DataTable.cs

Gets an array of all DataRow objects that match the filter criteria.

C#
public System.Data.DataRow[] Select(string? filterExpression);
C#
public System.Data.DataRow[] Select(string filterExpression);

Parameters

filterExpression
String

The criteria to use to filter the rows.

Returns

An array of DataRow objects.

Examples

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

C#
private void GetRowsByFilter()
{
    DataTable table = DataSet1.Tables["Orders"];
    // Presuming the DataTable has a column named Date.
    string expression;
    expression = "Date > #1/1/00#";
    DataRow[] foundRows;

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

    // Print column 0 of each returned row.
    for(int i = 0; i < foundRows.Length; i ++)
    {
        Console.WriteLine(foundRows[i][0]);
    }
}

Remarks

To create the filterExpression argument, use the same rules that apply to the DataColumn class's Expression property value for creating filters.

To ensure the proper sort order, specify sort criteria with Select(String, String) or Select(String, String, DataViewRowState).

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

See also

Applies to

.NET 9 and other versions
Product Versions
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

Select(String, String)

Source:
DataTable.cs
Source:
DataTable.cs
Source:
DataTable.cs

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

C#
public System.Data.DataRow[] Select(string? filterExpression, string? sort);
C#
public System.Data.DataRow[] Select(string filterExpression, string sort);

Parameters

filterExpression
String

The criteria to use to filter the rows.

sort
String

A string specifying the column and sort direction.

Returns

An array of DataRow objects matching the filter expression.

Examples

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

C#
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.
      table.Rows.Add(newRow);

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

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

      // 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++)
         Console.WriteLine(foundRows[i][2]);
   }
}

Remarks

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.

See also

Applies to

.NET 9 and other versions
Product Versions
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

Select(String, String, DataViewRowState)

Source:
DataTable.cs
Source:
DataTable.cs
Source:
DataTable.cs

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

C#
public System.Data.DataRow[] Select(string? filterExpression, string? sort, System.Data.DataViewRowState recordStates);
C#
public System.Data.DataRow[] Select(string filterExpression, string sort, System.Data.DataViewRowState recordStates);

Parameters

filterExpression
String

The criteria to use to filter the rows.

sort
String

A string specifying the column and sort direction.

recordStates
DataViewRowState

One of the DataViewRowState values.

Returns

An array of DataRow objects.

Examples

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

C#
private static void GetRowsByFilter()
{
    DataTable customerTable = new DataTable("Customers");
    // Add columns
    customerTable.Columns.Add("id", typeof(int));
    customerTable.Columns.Add("name", typeof(string));

    // Set PrimaryKey
    customerTable.Columns[ "id" ].Unique = true;
    customerTable.PrimaryKey = new DataColumn[]
        { customerTable.Columns["id"] };

    // Add ten rows
    for(int id=1; id<=10; id++)
    {
        customerTable.Rows.Add(
            new object[] { id, string.Format("customer{0}", id) });
    }
    customerTable.AcceptChanges();

    // Add another ten rows
    for(int id=11; id<=20; id++)
    {
        customerTable.Rows.Add(
            new object[] { id, string.Format("customer{0}", id) });
    }

    string expression;
    string sortOrder;

    expression = "id > 5";
    // Sort descending by column named CompanyName.
    sortOrder = "name DESC";
    // Use the Select method to find all rows matching the filter.
    DataRow[] foundRows =
        customerTable.Select(expression, sortOrder,
        DataViewRowState.Added);

    PrintRows(foundRows, "filtered rows");

    foundRows = customerTable.Select();
    PrintRows(foundRows, "all rows");
}

private static void PrintRows(DataRow[] rows, string label)
{
    Console.WriteLine("\n{0}", label);
    if(rows.Length <= 0)
    {
        Console.WriteLine("no rows found");
        return;
    }
    foreach(DataRow row in rows)
    {
        foreach(DataColumn column in row.Table.Columns)
        {
            Console.Write("\table {0}", row[column]);
        }
        Console.WriteLine();
    }
}

Remarks

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.

See also

Applies to

.NET 9 and other versions
Product Versions
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1