DataTable.Select メソッドとは? わかりやすく解説

DataTable.Select メソッド (String, String, DataViewRowState)

フィルタ基準一致するすべての DataRow オブジェクト配列を、指定した態と一致する並べ替え順に取得します

名前空間: System.Data
アセンブリ: System.Data (system.data.dll 内)
構文構文

Public Function Select (
 _
    filterExpression As String, _
    sort As String, _
    recordStates As DataViewRowState _
) As DataRow()
Dim instance As DataTable
Dim filterExpression As String
Dim sort As String
Dim recordStates As DataViewRowState
Dim returnValue As DataRow()

returnValue = instance.Select(filterExpression, sort, recordStates)
public DataRow[] Select (
    string filterExpression,
    string sort,
    DataViewRowState recordStates
)
public:
array<DataRow^>^ Select (
    String^ filterExpression, 
    String^ sort, 
    DataViewRowState recordStates
)
public DataRow[] Select (
    String filterExpression, 
    String sort, 
    DataViewRowState recordStates
)
public function Select (
    filterExpression : String, 
    sort : String, 
    recordStates : DataViewRowState
) : DataRow[]

パラメータ

filterExpression

行にフィルタをかけるために使用する基準

sort

列と並べ替え方向指定する文字列

recordStates

DataViewRowState 値の 1 つ

戻り値
DataRow オブジェクト配列

解説解説

引数 filterExpression作成するには、DataColumn クラスExpression プロパティの値を作成するときと同じ規則使用します引数 Sort は、クラスExpression 文字列作成するときと同じ規則使用します

使用例使用例

フィルタ式とレコードの状態を使用してDataRow オブジェクト配列返す例を次に示します

Private Sub GetRowsByFilter()

    Dim customerTable As DataTable = New
 DataTable("Customers")

    ' Add columns
    customerTable.Columns.Add("id", GetType(Integer))
    customerTable.Columns.Add("name", GetType(String))

    ' Set PrimaryKey
    customerTable.Columns("id").Unique = True
    customerTable.PrimaryKey = New DataColumn() _
        {customerTable.Columns("id")}

    ' add ten rows
    Dim id As Integer
    For id = 1 To 10
        customerTable.Rows.Add( _
        New Object() {id, String.Format("customer{0}",
 id)})
    Next id
    customerTable.AcceptChanges()

    ' add another ten rows
    For id = 11 To 20
        customerTable.Rows.Add( _
        New Object() {id, String.Format("customer{0}",
 id)})
    Next id

    Dim expression As String
    Dim sortOrder As String

    expression = "id > 5"
    ' Sort descending by CompanyName column.
    sortOrder = "name DESC"

    ' Use the Select method to find all rows matching the filter.
    Dim foundRows As DataRow() = _
        customerTable.Select(expression, sortOrder, _
        DataViewRowState.Added)

    PrintRows(foundRows, "filtered rows")

    foundRows = customerTable.Select()
    PrintRows(foundRows, "all rows")
End Sub

Private Sub PrintRows(ByVal
 rows() As DataRow, ByVal label As
 String)
    Console.WriteLine("\n{0}", label)
    If rows.Length <= 0 Then
        Console.WriteLine("no rows found")
        Exit Sub
    End If

    Dim row As DataRow
    Dim column As DataColumn
    For Each row In rows
        For Each column In
 row.Table.Columns
            Console.Write("\table {0}", row(column))
        Next column
        Console.WriteLine()
    Next row
End Sub
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();
    }
}
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照

DataTable.Select メソッド (String)

フィルタ基準一致するすべての DataRow オブジェクト主キーの順に (主キーない場合追加された順に) 配列として取得します

名前空間: System.Data
アセンブリ: System.Data (system.data.dll 内)
構文構文

Public Function Select (
 _
    filterExpression As String _
) As DataRow()
Dim instance As DataTable
Dim filterExpression As String
Dim returnValue As DataRow()

returnValue = instance.Select(filterExpression)
public DataRow[] Select (
    string filterExpression
)
public:
array<DataRow^>^ Select (
    String^ filterExpression
)
public DataRow[] Select (
    String filterExpression
)
public function Select (
    filterExpression : String
) : DataRow[]

パラメータ

filterExpression

行にフィルタをかけるために使用する基準

戻り値
DataRow オブジェクト配列

解説解説

引数 filterExpression作成するには、フィルタ作成するときに DataColumn クラスExpression プロパティの値に適用する規則と同じ規則使用します

使用例使用例

フィルタ式を使用して DataRow オブジェクト配列返す例を次に示します

Private Sub GetRowsByFilter()
    
    Dim table As DataTable = DataSet1.Tables("Orders")

    ' Presuming the DataTable has a column named Date.
    Dim expression As String
    expression = "Date > '1/1/00'"
    Dim foundRows() As DataRow

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

    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
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]);
    }
}
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照

DataTable.Select メソッド

DataTable の作成使用

DataTable.Select メソッド (String, String)

フィルタ基準一致するすべての DataRow オブジェクト配列を、指定した並べ替え順で取得します

名前空間: System.Data
アセンブリ: System.Data (system.data.dll 内)
構文構文

Public Function Select (
 _
    filterExpression As String, _
    sort As String _
) As DataRow()
Dim instance As DataTable
Dim filterExpression As String
Dim sort As String
Dim returnValue As DataRow()

returnValue = instance.Select(filterExpression, sort)
public DataRow[] Select (
    string filterExpression,
    string sort
)
public:
array<DataRow^>^ Select (
    String^ filterExpression, 
    String^ sort
)
public DataRow[] Select (
    String filterExpression, 
    String sort
)
public function Select (
    filterExpression : String, 
    sort : String
) : DataRow[]

パラメータ

filterExpression

行にフィルタをかけるために使用する基準

sort

列と並べ替え方向指定する文字列

戻り値
フィルタ式と一致する DataRow オブジェクト配列

解説解説

引数 filterExpression作成するには、DataColumn クラスExpression プロパティの値を作成するときと同じ規則使用します引数 Sort は、クラスExpression 文字列作成するときと同じ規則使用します

使用例使用例

フィルタ式を使用して DataRow オブジェクト配列返す例を次に示します

Private Sub GetRowsByFilter()
    Dim table As DataTable = DataSet1.Tables(0)

    ' Presuming the DataTable has a column named Date.
    Dim expression As String
 = "Date > 1/1/00"

    ' Sort descending by column named CompanyName.
    Dim sortOrder As String
 = "CompanyName DESC" 
    Dim foundRows() As DataRow

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

    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
private void GetRowsByFilter()
{
    DataTable table = DataSet1.Tables["Orders"];

    // Presuming the DataTable has a column named Date.
    string expression = "Date > '1/1/00'";

    // Sort descending by column named CompanyName.
    string sortOrder = "CompanyName DESC";
    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][0]);
    }
}
 
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照

DataTable.Select メソッド ()

すべての DataRow オブジェクト配列取得します

名前空間: System.Data
アセンブリ: System.Data (system.data.dll 内)
構文構文

解説解説
使用例使用例

Select メソッド使用して DataRow オブジェクト配列返す例を次に示します

Private Sub GetRows()
    ' Get the DataTable of a DataSet.
    Dim table As DataTable = DataSet1.Tables("Suppliers")
    Dim rows() As DataRow = table.Select()

    Dim i As Integer
    ' Print the value one column of each DataRow.
    For i = 0 to rows.GetUpperBound(0)
       Console.WriteLine(rows(i)("CompanyName"))
    Next i
End Sub
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"]);
    }
}
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照



英和和英テキスト翻訳>> Weblio翻訳
英語⇒日本語日本語⇒英語
  

辞書ショートカット

カテゴリ一覧

すべての辞書の索引



Weblioのサービス

「DataTable.Select メソッド」の関連用語


DataTable.Select メソッドのお隣キーワード
検索ランキング

   

英語⇒日本語
日本語⇒英語
   



DataTable.Select メソッドのページの著作権
Weblio 辞書 情報提供元は 参加元一覧 にて確認できます。

   
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2024 Microsoft.All rights reserved.

©2024 GRAS Group, Inc.RSS