DataTable.Select 方法 (String, String, DataViewRowState)

本文详细介绍了 DataTable.Select 方法,该方法可获取与排序顺序中的筛选器以及指定状态相匹配的所有 DataRow 对象的数组。文中说明了参数、返回值,给出备注和示例,还提及了使用该方法的平台要求及相关参考内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

DataTable.Select 方法 (String, String, DataViewRowState)

获取与排序顺序中的筛选器以及指定的状态相匹配的所有 DataRow 对象的数组。

[Visual Basic]
Overloads Public Function Select( _
   ByVal filterExpression As String, _
   ByVal sort As String, _
   ByVal recordStates As DataViewRowState _
) As DataRow()
[C#]
public DataRow[] Select(
   string filterExpression,
   string sort,
   DataViewRowState recordStates
);
[C++]
public: DataRow* Select(
   String* filterExpression,
   String* sort,
   DataViewRowState recordStates
) [];
[JScript]
public function Select(
   filterExpression : String,
   sort : String,
   recordStates : DataViewRowState
) : DataRow[];
参数
filterExpression
要用来筛选行的条件。
sort
一个字符串,它指定列和排序方向。
recordStates
DataViewRowState 值之一。
返回值

DataRow 对象的数组。

备注

若要形成 filterExpression 参数,请使用与创建 DataColumn 类的 Expression 属性值相同的规则。Sort 参数也使用与创建类的 Expression 字符串相同的规则。

示例

[Visual Basic, C#, C++] 以下示例使用筛选表达式和记录状态来返回 DataRow 对象的数组。

[Visual Basic] 
Private Sub GetRowsByFilter()
        Dim customerTable As DataTable
    customerTable = new DataTable( "Customers" )

    ' Add columns
    customerTable.Columns.Add( "id", GetType(Integer) )
    customerTable.Columns.Add( "name", GetType(String) )

    ' Set PrimaryKey
    customerTable.Columns("id").Unique = true
    customerTable.PrimaryKey = new DataColumn() { customerTable.Columns("id") }

    ' add ten rows
    Dim id As Integer
    For id = 1 To 10
        customerTable.Rows.Add( _
            new object() { id, string.Format("customer{0}", id) } )
    Next id
    customerTable.AcceptChanges()

    ' add another ten rows
    For id = 11 To 20
        customerTable.Rows.Add( _
            new object() { id, string.Format("customer{0}", id) } )
    Next id

    Dim strExpr As String
    Dim strSort As String
        strExpr = "id > 5"
    ' Sort descending by CompanyName column.
    strSort = "name DESC"
    ' Use the Select method to find all rows matching the filter.
    Dim foundRows As DataRow() = _
        customerTable.Select( strExpr, strSort, DataViewRowState.Added )
        PrintRows( foundRows, "filtered rows")

    foundRows = customerTable.Select()
    PrintRows( foundRows, "all rows")
End Sub

Private Sub PrintRows( rows() As DataRow, label As String)
    Console.WriteLine( "/n{0}", label )
    If rows.Length <= 0 Then
        Console.WriteLine( "no rows found" )
        Exit Sub
    End If
    Dim r As DataRow
    Dim c As DataColumn
    For Each r In rows
        For Each c In r.Table.Columns
            Console.Write( "/t {0}", r(c) )
        Next c
        Console.WriteLine()
    Next r
End Sub

[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 strExpr;
    string strSort;
        strExpr = "id > 5";
    // Sort descending by column named CompanyName.
    strSort = "name DESC";
    // Use the Select method to find all rows matching the filter.
    DataRow[] foundRows = 
        customerTable.Select( strExpr, strSort, 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 r in rows )
    {
        foreach( DataColumn c in r.Table.Columns )
        {
            Console.Write( "/t {0}", r[c] );
        }
        Console.WriteLine();
    }
}

[C++] 
private:
static void GetRowsByFilter()
{
        DataTable* customerTable = new DataTable( S"Customers" );
    // Add columns
    customerTable->Columns->Add( S"id", __typeof(int) );
    customerTable->Columns->Add( S"name", __typeof(String) );

    // Set PrimaryKey
    customerTable->Columns->Item[ S"id" ]->Unique = true;

    DataColumn* temp2 [] = {customerTable->Columns->Item[S"id"]};
    customerTable->PrimaryKey = temp2;

    // Add ten rows
    for( int id=1; id<=10; id++ )
    {
        Object* temp0 [] = {__box(id), String::Format(S"customer{0}", __box(id))};
        customerTable->Rows->Add( temp0 );
    }
    customerTable->AcceptChanges();

    // Add another ten rows
    for( int id=11; id<=20; id++ )
    {
        Object* temp1 [] = {__box(id), String::Format(S"customer{0}", __box(id))};
        customerTable->Rows->Add( temp1 );
    }

    String* strExpr;
    String* strSort;
        strExpr = S"id > 5";
    // Sort descending by column named CompanyName.
    strSort = S"name DESC";
    // Use the Select method to find all rows matching the filter.
    DataRow* foundRows[] = 
        customerTable->Select( strExpr, strSort, DataViewRowState::Added );
        PrintRows( foundRows, S"filtered rows" );

    foundRows = customerTable->Select();
    PrintRows( foundRows, S"all rows" );
}

static void PrintRows( DataRow* rows[], String* label )
{
    Console::WriteLine( S"/n{0}", label );
    if( rows->Length <= 0 )
    {
        Console::WriteLine( S"no rows found" );
        return;
    }
    System::Collections::IEnumerator* myEnum = rows->GetEnumerator();
    while (myEnum->MoveNext())
    {
        DataRow* r = __try_cast<DataRow*>(myEnum->Current);
        System::Collections::IEnumerator* myEnum1 = r->Table->Columns->GetEnumerator();
        while (myEnum1->MoveNext())
        {
            DataColumn* c = __try_cast<DataColumn*>(myEnum1->Current);
            Console::Write( S"/t {0}", r->Item[c] );
        }
        Console::WriteLine();
    }
}

[JScript] 没有可用于 JScript 的示例。若要查看 Visual Basic、C# 或 C++ 示例,请单击页左上角的“语言筛选器”按钮 语言筛选器

要求

平台: Windows 98, Windows NT 4.0, Windows ME, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 系列, .NET Framework 精简版

请参见

DataTable 类 | DataTable 成员 | System.Data 命名空间 | DataTable.Select 重载列表 | CaseSensitive | DataRow | DataView | DataViewRowState | Expression

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值