Leer en inglés

Compartir vía


DataTable.Select Método

Definición

Obtiene una matriz de DataRow objetos.

Sobrecargas

Select()

Obtiene una matriz de todos los objetos DataRow.

Select(String)

Obtiene una matriz de todos los objetos DataRow que coinciden con los criterios de filtro.

Select(String, String)

Obtiene una matriz de todos los objetos DataRow que coinciden con los criterios de filtro, en el criterio de ordenación especificado.

Select(String, String, DataViewRowState)

Obtiene una matriz de todos los objetos DataRow que coinciden con el filtro en el orden de clasificación y que coinciden con el estado especificado.

Select()

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

Obtiene una matriz de todos los objetos DataRow.

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

Devoluciones

Matriz de objetos DataRow.

Ejemplos

En el ejemplo siguiente se devuelve una matriz de DataRow objetos a través del Select método .

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

Comentarios

Para garantizar el criterio de ordenación adecuado, especifique criterios de ordenación con Select(String, String) o Select(String, String, DataViewRowState).

Consulte también

Se aplica a

.NET 9 otras versiones
Producto Versiones
.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

Obtiene una matriz de todos los objetos DataRow que coinciden con los criterios de filtro.

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

Parámetros

filterExpression
String

Criterios que se van a utilizar para filtrar las filas.

Devoluciones

Matriz de objetos DataRow.

Ejemplos

En el ejemplo siguiente se usa una expresión de filtro para devolver una matriz de DataRow objetos.

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

Comentarios

Para crear el filterExpression argumento, use las mismas reglas que se aplican al valor de propiedad de Expression la DataColumn clase para crear filtros.

Para garantizar el criterio de ordenación adecuado, especifique criterios de ordenación con Select(String, String) o Select(String, String, DataViewRowState).

Si la columna del filtro contiene un valor NULL, no formará parte del resultado.

Consulte también

Se aplica a

.NET 9 otras versiones
Producto Versiones
.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

Obtiene una matriz de todos los objetos DataRow que coinciden con los criterios de filtro, en el criterio de ordenación especificado.

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

Parámetros

filterExpression
String

Criterios que se van a utilizar para filtrar las filas.

sort
String

Cadena que especifica la columna y orden de clasificación.

Devoluciones

Matriz de objetos DataRow que coinciden con la expresión de filtro.

Ejemplos

En el ejemplo siguiente se usa una expresión de filtro para devolver una matriz de DataRow objetos.

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

Comentarios

Para formar el filterExpression argumento, use las mismas reglas para crear el valor de propiedad de Expression la DataColumn clase. El Sort argumento también usa las mismas reglas para crear las cadenas de la Expression clase.

Si la columna del filtro contiene un valor NULL, no formará parte del resultado.

Consulte también

Se aplica a

.NET 9 otras versiones
Producto Versiones
.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

Obtiene una matriz de todos los objetos DataRow que coinciden con el filtro en el orden de clasificación y que coinciden con el estado especificado.

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

Parámetros

filterExpression
String

Criterios que se van a utilizar para filtrar las filas.

sort
String

Cadena que especifica la columna y orden de clasificación.

recordStates
DataViewRowState

Uno de los valores de DataViewRowState.

Devoluciones

Matriz de objetos DataRow.

Ejemplos

En el ejemplo siguiente se usa una expresión de filtro y un estado de registro para devolver una matriz de DataRow objetos.

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

Comentarios

Para formar el filterExpression argumento, use las mismas reglas para crear el valor de propiedad de Expression la DataColumn clase. El Sort argumento también usa las mismas reglas para crear las cadenas de la Expression clase.

Si la columna del filtro contiene un valor NULL, no formará parte del resultado.

Consulte también

Se aplica a

.NET 9 otras versiones
Producto Versiones
.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