Share via


方法: エンティティ型オブジェクトを返すクエリを実行する (Entity Framework)

このトピックでは、エンティティのコレクションを返すクエリの実行方法について、例を示します。 この例ではさらに、Contacts のコレクションを反復処理し、各 Contact の姓と名を表示します。 . オブジェクトを 1 つだけ返すには、クエリの先頭に、FirstFirstOrDefaultSingle、または SingleOrDefault のいずれかのメソッドを使用します。

次の各 Entity Framework クエリ テクノロジを使用して同じ処理を実行する例を示します。

  • LINQ to Entities

  • Entity SQL と ObjectQuery<T>

  • ObjectQuery<T> のクエリ ビルダー メソッド

このトピックの例には、Adventure Works Sales Model が使用されています。このトピックのコードを実行するには、あらかじめプロジェクトに Adventure Works Sales Model を追加し、Entity Framework を使用するようにプロジェクトを構成しておく必要があります。詳細については、「Entity Data Model ウィザードを使用する方法 (Entity Framework)」、または「Entity Framework プロジェクトを手動で構成する方法」、および「Entity Data Model を手動で定義する方法 (Entity Framework)」を参照してください。

以下は LINQ to Entities の例です。

Using context As New AdventureWorksEntities()
    Dim LastName = "Zhou"
    Dim query = From contact In context.Contacts Where contact.LastName = LastName

    ' Iterate through the collection of Contact items.
    For Each result As Contact In query
        Console.WriteLine("Contact First Name: {0}; Last Name: {1}", _
                result.FirstName, result.LastName)
    Next
End Using
using (AdventureWorksEntities context = new AdventureWorksEntities())
{
    string LastName = "Zhou";
    var query = from contact in context.Contacts where 
                    contact.LastName == LastName select contact;

    // Iterate through the collection of Contact items.
    foreach( var result in query)
    {
        Console.WriteLine("Contact First Name: {0}; Last Name: {1}", 
                result.FirstName, result.LastName);
    }
}

以下は Entity SQL の例です。

Using context As New AdventureWorksEntities()

    Dim esqlString As String = "SELECT VALUE Contact " & _
        "FROM AdventureWorksEntities.Contacts as Contact where Contact.LastName = @ln"

    Dim query As New ObjectQuery(Of Contact)(esqlString, context, MergeOption.NoTracking)
    query.Parameters.Add(New ObjectParameter("ln", "Zhou"))

    ' Iterate through the collection of Contact items. 
    For Each result As Contact In query
        Console.WriteLine("Contact First Name: {0}; Last Name: {1}", _
                result.FirstName, result.LastName)
    Next
End Using
using (AdventureWorksEntities context =
    new AdventureWorksEntities())
{
    string esqlQuery = @"SELECT VALUE Contact
        FROM AdventureWorksEntities.Contacts as Contact where Contact.LastName = @ln";

    // The following query returns a collection of Contact objects.
    ObjectQuery<Contact> query = new ObjectQuery<Contact>(esqlQuery, context, MergeOption.NoTracking);
    query.Parameters.Add(new ObjectParameter("ln", "Zhou"));

    // Iterate through the collection of Contact items.
    foreach (Contact result in query)
        Console.WriteLine("Contact First Name: {0}; Last Name: {1}",
                result.FirstName, result.LastName);
}

以下は、クエリ ビルダー メソッドの例です。

Using context As New AdventureWorksEntities()
    Dim query As ObjectQuery(Of Contact) = _
        context.Contacts.Where("it.LastName==@ln",
        New ObjectParameter("ln", "Zhou"))

    ' Iterate through the collection of Contact items.
    For Each result As Contact In query
        Console.WriteLine("Contact First Name: {0}; Last Name: {1}", _
                result.FirstName, result.LastName)
    Next
End Using
using (AdventureWorksEntities context =
    new AdventureWorksEntities())
{
    ObjectQuery<Contact> query= context.Contacts.Where("it.LastName==@ln", 
        new ObjectParameter("ln", "Zhou"));

    // Iterate through the collection of Contact items.
    foreach (Contact result in query)
        Console.WriteLine("Contact First Name: {0}; Last Name: {1}",
                result.FirstName, result.LastName);
}

参照

処理手順

方法: 匿名型のコレクションを返すクエリを実行する (Entity Framework)
方法: プリミティブ型のコレクションを返すクエリを実行する (Entity Framework)
パラメーター化クエリを実行する方法 (Entity Framework)

その他のリソース

高度なデータ モデルの定義 (Entity Framework タスク)