次の方法で共有


DataTable.Select メソッド (String, String, DataViewRowState)

フィルタ基準と一致するすべての DataRow オブジェクトの配列を、指定した状態と一致する並べ替え順に取得します。

Overloads Public Function Select( _
   ByVal filterExpression As String, _   ByVal sort As String, _   ByVal recordStates As DataViewRowState _) As DataRow()
[C#]
public DataRow[] Select(stringfilterExpression,stringsort,DataViewRowStaterecordStates);
[C++]
public: DataRow* Select(String* filterExpression,String* sort,DataViewRowStaterecordStates) [];
[JScript]
public function Select(
   filterExpression : String,sort : String,recordStates : DataViewRowState) : DataRow[];

パラメータ

  • filterExpression
    行にフィルタをかけるために使用する基準。
  • sort
    列と並べ替え方向を指定する文字列。
  • recordStates
    DataViewRowState 値の 1 つ。

戻り値

DataRow オブジェクトの配列。

解説

引数 filterExpression を作成するには、 DataColumn クラスの Expression プロパティの値を作成するときと同じ規則を使用します。引数 Sort は、クラスの Expression 文字列を作成するときと同じ規則も使用します。

使用例

[Visual Basic, C#, C++] フィルタ式とレコードの状態を使用して、 DataRow オブジェクトの配列を返す例を次に示します。

 
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 Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ, .NET Compact Framework - Windows CE .NET

参照

DataTable クラス | DataTable メンバ | System.Data 名前空間 | DataTable.Select オーバーロードの一覧 | CaseSensitive | DataRow | DataView | DataViewRowState | Expression