获取与排序顺序中的筛选器以及指定的状态相匹配的所有 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