Language

DataRowCollection.Find メソッド

定義

指定したDataRow値を使用してPrimaryKeyを取得します。

オーバーロード

名前 説明
Find(Object[])

指定した主キー値を含む行を取得します。

Find(Object)

主キー値で指定された行を取得します。

注釈

パフォーマンスは O(log n) 操作である必要があります。

Find(Object[])

ソース:
DataRowCollection.cs
ソース:
DataRowCollection.cs
ソース:
DataRowCollection.cs
ソース:
DataRowCollection.cs
ソース:
DataRowCollection.cs

指定した主キー値を含む行を取得します。

public:
 System::Data::DataRow ^ Find(cli::array <System::Object ^> ^ keys);
public System.Data.DataRow? Find(object?[] keys);
public System.Data.DataRow Find(object[] keys);
member this.Find : obj[] -> System.Data.DataRow
Public Function Find (keys As Object()) As DataRow

パラメーター

keys
Object[]

検索する主キー値の配列。 配列の型は Object。

返品

指定された主キー値を含む DataRow オブジェクト。それ以外の場合は、主キー値が DataRowCollectionに存在しない場合は null 値です。

例外

そのインデックス値に対応する行はありません。

テーブルに主キーがありません。

例

次の例では、配列の値を使用して、 DataRow オブジェクトのコレクション内の特定の行を検索します。 このメソッドは、3 つの主キー列を持つ DataTable が存在することを前提としています。 値の配列を作成した後、コードは配列と共に Find メソッドを使用して、必要な特定のオブジェクトを取得します。

private void FindInMultiPKey(DataTable table)
{
    // Create an array for the key values to find.
    object[]findTheseVals = new object[3];

    // Set the values of the keys to find.
    findTheseVals[0] = "John";
    findTheseVals[1] = "Smith";
    findTheseVals[2] = "5 Main St.";

    DataRow foundRow = table.Rows.Find(findTheseVals);
    // Display column 1 of the found row.
    if(foundRow != null)
        Console.WriteLine(foundRow[1]);
}
 Private Sub FindInMultiPKey(ByVal table As DataTable)
    ' Create an array for the key values to find.
    Dim findTheseVals(2) As Object

    ' Set the values of the keys to find.
    findTheseVals(0) = "John"
    findTheseVals(1) = "Smith"
    findTheseVals(2) = "5 Main St."

    Dim foundRow As DataRow = table.Rows.Find(findTheseVals)
    ' Display column 1 of the found row.
    If Not (foundRow Is Nothing) Then
        Console.WriteLine(foundRow(1).ToString())
    End If
End Sub

注釈

Find メソッドを使用するには、DataTable オブジェクトが属するDataRowCollection オブジェクトに、主キー列として指定された列が少なくとも 1 つ必要です。 2 つ以上の行が同じ主キー値を持つ場合は、最初に見つかった行が返されます。 これは、 EnforceConstraints が false に設定されている場合に発生します。 テーブルに複数の主キーがある場合に、PrimaryKey列またはPrimaryKey オブジェクトの配列を作成する方法の詳細については、DataColumn プロパティを参照してください。

こちらもご覧ください

適用対象

Find(Object)

ソース:
DataRowCollection.cs
ソース:
DataRowCollection.cs
ソース:
DataRowCollection.cs
ソース:
DataRowCollection.cs
ソース:
DataRowCollection.cs

主キー値で指定された行を取得します。

public:
 System::Data::DataRow ^ Find(System::Object ^ key);
public System.Data.DataRow? Find(object? key);
public System.Data.DataRow Find(object key);
member this.Find : obj -> System.Data.DataRow
Public Function Find (key As Object) As DataRow

パラメーター

key
Object

検索する DataRow の主キー値。

返品

指定された主キー値を含む DataRow 。それ以外の場合は、主キー値が DataRowCollectionに存在しない場合は null 値。

例外

テーブルに主キーがありません。

例

次の例では、 Find メソッドを使用して、 DataRow オブジェクトのコレクションで主キー値 "2" を検索します。 このメソッドは、必要に応じて値を変更できる特定の DataRow オブジェクトを返します。

private void FindInPrimaryKeyColumn(DataTable table,
    long pkValue)
{
    // Find the number pkValue in the primary key
    // column of the table.
    DataRow foundRow = table.Rows.Find(pkValue);

    // Print the value of column 1 of the found row.
    if(foundRow != null)
        Console.WriteLine(foundRow[1]);
}
 Private Sub FindInPrimaryKeyColumn(ByVal table As DataTable, _
    ByVal pkValue As Long)
    ' Find the number pkValue in the primary key 
    ' column of the table.
    Dim foundRow As DataRow = table.Rows.Find(pkValue)

    ' Print the value of column 1 of the found row.
    If Not (foundRow Is Nothing) Then
        Console.WriteLine(foundRow(1).ToString())
    End If
End Sub

注釈

Find メソッドを使用するには、DataTable オブジェクトが属するDataRowCollection オブジェクトに、主キー列として指定された列が少なくとも 1 つ必要です。 主キー列を作成する方法の詳細については、 PrimaryKey プロパティを参照してください。

こちらもご覧ください

適用対象