Ler em inglês

Compartilhar via


DataTable.Select Método

Definição

Obtém uma matriz de objetos DataRow.

Sobrecargas

Select()

Obtém uma matriz de todos os objetos DataRow.

Select(String)

Obtém uma matriz de todos os objetos DataRow que correspondem aos critérios de filtro.

Select(String, String)

Obtém uma matriz de todos os objetos DataRow que correspondem aos critérios de filtro, na ordem de classificação especificada.

Select(String, String, DataViewRowState)

Obtém uma matriz de todos os objetos DataRow que correspondem ao filtro, na ordem de classificação correspondente ao estado especificado.

Select()

Origem:
DataTable.cs
Origem:
DataTable.cs
Origem:
DataTable.cs

Obtém uma matriz de todos os objetos DataRow.

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

Retornos

Uma matriz de objetos de DataRow.

Exemplos

O exemplo a seguir retorna uma matriz de DataRow objetos por meio do Select método .

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"]);
    }
}

Comentários

Para garantir a ordem de classificação adequada, especifique critérios de classificação com Select(String, String) ou Select(String, String, DataViewRowState).

Confira também

Aplica-se a

.NET 9 e outras versões
Produto Versões
.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)

Origem:
DataTable.cs
Origem:
DataTable.cs
Origem:
DataTable.cs

Obtém uma matriz de todos os objetos DataRow que correspondem aos critérios de filtro.

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

Parâmetros

filterExpression
String

Os critérios a serem usados para filtrar as linhas.

Retornos

Uma matriz de objetos de DataRow.

Exemplos

O exemplo a seguir usa uma expressão de filtro para retornar uma matriz de DataRow objetos .

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

Comentários

Para criar o filterExpression argumento, use as mesmas regras que se aplicam ao DataColumn valor da propriedade da Expression classe para criar filtros.

Para garantir a ordem de classificação adequada, especifique critérios de classificação com Select(String, String) ou Select(String, String, DataViewRowState).

Se a coluna no filtro contiver um valor nulo, ela não fará parte do resultado.

Confira também

Aplica-se a

.NET 9 e outras versões
Produto Versões
.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)

Origem:
DataTable.cs
Origem:
DataTable.cs
Origem:
DataTable.cs

Obtém uma matriz de todos os objetos DataRow que correspondem aos critérios de filtro, na ordem de classificação especificada.

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

Parâmetros

filterExpression
String

Os critérios a serem usados para filtrar as linhas.

sort
String

Uma cadeia de caracteres que especifica a direção da coluna e da classificação.

Retornos

Uma matriz de objetos DataRow que correspondem à expressão de filtro.

Exemplos

O exemplo a seguir usa uma expressão de filtro para retornar uma matriz de DataRow objetos .

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

Comentários

Para formar o filterExpression argumento , use as mesmas regras para criar o DataColumn valor da propriedade da Expression classe. O Sort argumento também usa as mesmas regras para criar cadeias de caracteres da Expression classe.

Se a coluna no filtro contiver um valor nulo, ela não fará parte do resultado.

Confira também

Aplica-se a

.NET 9 e outras versões
Produto Versões
.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)

Origem:
DataTable.cs
Origem:
DataTable.cs
Origem:
DataTable.cs

Obtém uma matriz de todos os objetos DataRow que correspondem ao filtro, na ordem de classificação correspondente ao estado especificado.

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

Parâmetros

filterExpression
String

Os critérios a serem usados para filtrar as linhas.

sort
String

Uma cadeia de caracteres que especifica a direção da coluna e da classificação.

recordStates
DataViewRowState

Um dos valores de DataViewRowState.

Retornos

Uma matriz de objetos de DataRow.

Exemplos

O exemplo a seguir usa uma expressão de filtro e um estado de registro para retornar uma matriz de DataRow objetos .

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

Comentários

Para formar o filterExpression argumento , use as mesmas regras para criar o DataColumn valor da propriedade da Expression classe. O Sort argumento também usa as mesmas regras para criar cadeias de caracteres da Expression classe.

Se a coluna no filtro contiver um valor nulo, ela não fará parte do resultado.

Confira também

Aplica-se a

.NET 9 e outras versões
Produto Versões
.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