閱讀英文

共用方式為


DataTable.Select 方法

定義

取得 DataRow 物件的陣列。

多載

Select()

取得所有 DataRow 物件的陣列。

Select(String)

取得所有符合篩選準則之 DataRow 物件的陣列。

Select(String, String)

取得按照指定排序順序符合篩選條件的所有 DataRow 物件之陣列。

Select(String, String, DataViewRowState)

取得符合篩選條件 (按照排序順序,並符合指定狀態) 的所有 DataRow 物件之陣列。

Select()

來源:
DataTable.cs
來源:
DataTable.cs
來源:
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, DataViewRowState)指定排序準則Select(String, String)

另請參閱

適用於

.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)

來源:
DataTable.cs
來源:
DataTable.cs
來源:
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, DataViewRowState)指定排序準則Select(String, String)

如果篩選上的數據行包含 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)

來源:
DataTable.cs
來源:
DataTable.cs
來源:
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)

來源:
DataTable.cs
來源:
DataTable.cs
來源:
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