获取按照指定的排序顺序且与筛选条件相匹配的所有 DataRow 对象的数组。
[Visual Basic] Overloads Public Function Select( _ ByVal filterExpression As String, _ ByVal sort As String _ ) As DataRow() [C#] public DataRow[] Select( string filterExpression, string sort ); [C++] public: DataRow* Select( String* filterExpression, String* sort ) []; [JScript] public function Select( filterExpression : String, sort : String ) : DataRow[];
参数
-
filterExpression
- 要用来筛选行的条件。 sort
- 一个字符串,它指定列和排序方向。
返回值
与筛选表达式相匹配的 DataRow 对象的数组。
备注
若要形成 filterExpression 参数,请使用与创建 DataColumn 类的 Expression 属性值相同的规则。Sort 参数也使用与创建类的 Expression 字符串相同的规则。
示例
[Visual Basic, C#, C++] 以下示例使用筛选表达式来返回 DataRow 对象的数组。
[Visual Basic] Private Sub GetRowsByFilter() Dim t As DataTable t = DataSet1.Tables(0) ' Presuming the DataTable has a column named Date. Dim strExpr As String Dim strSort As String strExpr = "Date > 1/1/00" ' Sort descending by column named CompanyName. strSort = "CompanyName DESC" Dim foundRows() As DataRow ' Use the Select method to find all rows matching the filter. foundRows = t.Select(strExpr, strSort) Dim i As Integer ' Print column 0 of each returned row. For i = 0 to foundRows.GetUpperBound(0) Console.WriteLine(foundRows(i)(0)) Next i End Sub [C#] private void GetRowsByFilter(){ DataTable myTable; myTable = DataSet1.Tables["Orders"]; // Presuming the DataTable has a column named Date. string strExpr; string strSort; strExpr = "Date > '1/1/00'"; // Sort descending by column named CompanyName. strSort = "CompanyName DESC"; DataRow[] foundRows; // Use the Select method to find all rows matching the filter. foundRows = myTable.Select(strExpr, strSort); // Print column 0 of each returned row. for(int i = 0; i < foundRows.Length; i ++){ Console.WriteLine(foundRows[i][0]); } } [C++] private: void GetRowsByFilter(){ DataTable* myTable; myTable = DataSet1->Tables->Item[S"Orders"]; // Presuming the DataTable has a column named Date. String* strExpr; String* strSort; strExpr = S"Date > '1/1/00'"; // Sort descending by column named CompanyName. strSort = S"CompanyName DESC"; DataRow* foundRows[]; // Use the Select method to find all rows matching the filter. foundRows = myTable->Select(strExpr, strSort); // Print column 0 of each returned row. for(int i = 0; i < foundRows->Length; i ++){ Console::WriteLine(foundRows[i]->Item[0]); } }
[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 | Expression | DataRow