DataTable.Select Method (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.

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

public DataRow[] Select(
	string filterExpression,
	string sort,
	DataViewRowState recordStates
)

Parameters

filterExpression
Type: System.String
The criteria to use to filter the rows.
sort
Type: System.String
A string specifying the column and sort direction.
recordStates
Type: System.Data.DataViewRowState
One of the DataViewRowState values.

Return Value

Type: System.Data.DataRow[]
An array of DataRow objects.

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 and record state to return an array of DataRow objects.


    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();
        }
    }


.NET Framework

Supported in: 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft