DataTable.Select 方法

定义

获取 DataRow 对象数组。

重载

Select()

获取由所有 DataRow 对象组成的数组。

Select(String)

获取由与筛选条件匹配的所有 DataRow 对象组成的数组。

Select(String, String)

以指定排序顺序,获取由与筛选条件匹配的所有 DataRow 对象组成的数组。

Select(String, String, DataViewRowState)

以与指定状态匹配的排序顺序,获取由与筛选条件匹配的所有 DataRow 对象组成的数组。

Select()

Source:
DataTable.cs
Source:
DataTable.cs
Source:
DataTable.cs

获取由所有 DataRow 对象组成的数组。

C#
public System.Data.DataRow[] Select ();

返回

一个 DataRow 对象数组。

示例

以下示例通过 Select 方法返回 对象的数组DataRow

C#
private void GetRows()
{
    // Get the DataTable of a DataSet.
    DataTable table = DataSet1.Tables["Suppliers"];
    DataRow[] rows = table.Select();

    // Print the value one column of each DataRow.
    for(int i = 0; i < rows.Length ; i++)
    {
        Console.WriteLine(rows[i]["CompanyName"]);
    }
}

注解

若要确保正确的排序顺序,请使用 Select(String, String)Select(String, String, DataViewRowState)指定排序条件。

另请参阅

适用于

.NET 9 和其他版本
产品 版本
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

Select(String)

Source:
DataTable.cs
Source:
DataTable.cs
Source:
DataTable.cs

获取由与筛选条件匹配的所有 DataRow 对象组成的数组。

C#
public System.Data.DataRow[] Select (string? filterExpression);
C#
public System.Data.DataRow[] Select (string filterExpression);

参数

filterExpression
String

用于筛选行的条件。

返回

一个 DataRow 对象数组。

示例

以下示例使用筛选器表达式返回对象的数组 DataRow

C#
private void GetRowsByFilter()
{
    DataTable table = DataSet1.Tables["Orders"];
    // Presuming the DataTable has a column named Date.
    string expression;
    expression = "Date > #1/1/00#";
    DataRow[] foundRows;

    // Use the Select method to find all rows matching the filter.
    foundRows = table.Select(expression);

    // Print column 0 of each returned row.
    for(int i = 0; i < foundRows.Length; i ++)
    {
        Console.WriteLine(foundRows[i][0]);
    }
}

注解

若要创建 filterExpression 参数,请使用适用于 DataColumn 类的 Expression 属性值的相同规则来创建筛选器。

若要确保正确的排序顺序,请使用 Select(String, String)Select(String, String, DataViewRowState)指定排序条件。

如果筛选器上的列包含 null 值,则它不会是结果的一部分。

另请参阅

适用于

.NET 9 和其他版本
产品 版本
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

Select(String, String)

Source:
DataTable.cs
Source:
DataTable.cs
Source:
DataTable.cs

以指定排序顺序,获取由与筛选条件匹配的所有 DataRow 对象组成的数组。

C#
public System.Data.DataRow[] Select (string? filterExpression, string? sort);
C#
public System.Data.DataRow[] Select (string filterExpression, string sort);

参数

filterExpression
String

用于筛选行的条件。

sort
String

指定列和排序方向的字符串。

返回

由与筛选条件匹配的 DataRow 对象组成的数组。

示例

以下示例使用筛选器表达式返回对象的数组 DataRow

C#
using System;
using System.Data;

public class A {

   public static void Main() {
      DataTable table = new DataTable("Orders");
      table.Columns.Add("OrderID", typeof(Int32));
      table.Columns.Add("OrderQuantity", typeof(Int32));
      table.Columns.Add("CompanyName", typeof(string));
      table.Columns.Add("Date", typeof(DateTime));

      DataRow newRow = table.NewRow();
      newRow["OrderID"] = 1;
      newRow["OrderQuantity"] = 3;
      newRow["CompanyName"] = "NewCompanyName";
      newRow["Date"] = "1979, 1, 31";

      // Add the row to the rows collection.
      table.Rows.Add(newRow);

      DataRow newRow2 = table.NewRow();
      newRow2["OrderID"] = 2;
      newRow2["OrderQuantity"] = 2;
      newRow2["CompanyName"] = "NewCompanyName1";
      table.Rows.Add(newRow2);

      DataRow newRow3 = table.NewRow();
      newRow3["OrderID"] = 3;
      newRow3["OrderQuantity"] = 2;
      newRow3["CompanyName"] = "NewCompanyName2";
      table.Rows.Add(newRow3);

      // Presuming the DataTable has a column named Date.
      string expression = "Date = '1/31/1979' or OrderID = 2";
      // string expression = "OrderQuantity = 2 and OrderID = 2";

      // Sort descending by column named CompanyName.
      string sortOrder = "CompanyName ASC";
      DataRow[] foundRows;

      // Use the Select method to find all rows matching the filter.
      foundRows = table.Select(expression, sortOrder);

      // Print column 0 of each returned row.
      for (int i = 0; i < foundRows.Length; i++)
         Console.WriteLine(foundRows[i][2]);
   }
}

注解

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

如果筛选器上的列包含 null 值,则它不会是结果的一部分。

另请参阅

适用于

.NET 9 和其他版本
产品 版本
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

Select(String, String, DataViewRowState)

Source:
DataTable.cs
Source:
DataTable.cs
Source:
DataTable.cs

以与指定状态匹配的排序顺序,获取由与筛选条件匹配的所有 DataRow 对象组成的数组。

C#
public System.Data.DataRow[] Select (string? filterExpression, string? sort, System.Data.DataViewRowState recordStates);
C#
public System.Data.DataRow[] Select (string filterExpression, string sort, System.Data.DataViewRowState recordStates);

参数

filterExpression
String

用于筛选行的条件。

sort
String

指定列和排序方向的字符串。

recordStates
DataViewRowState

DataViewRowState 值之一。

返回

一个 DataRow 对象数组。

示例

以下示例使用筛选器表达式和记录状态返回对象的数组 DataRow

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

注解

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

如果筛选器上的列包含 null 值,则它不会是结果的一部分。

另请参阅

适用于

.NET 9 和其他版本
产品 版本
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1