Gewusst wie: Ausführen einer parametrisierten Abfrage (Entity Framework)

In diesem Thema wird gezeigt, wie eine Entity SQL -Abfrage mit Parametern mithilfe von ObjectQuery ausgeführt wird. Im Beispiel werden zwei Parameter an ObjectQuery übergeben. Daraufhin wird die Abfrage ausgeführt und die Auflistung von Contact-Elementen durchlaufen. Dasselbe Beispiel wird bei der Verwendung der folgenden Entity Framework -Abfragetechnologien veranschaulicht:

  • LINQ to Entities

  • Entity SQL mit ObjectQuery<T>

  • Abfrage-Generator-Methoden von ObjectQuery<T>

Das Beispiel in diesem Thema beruht auf dem Adventure Works Sales-Modell. Zum Ausführen des Codes in diesem Thema muss dem Projekt bereits das Adventure Works Sales-Modell hinzugefügt und das Projekt zur Verwendung von Entity Framework konfiguriert worden sein. Weitere Informationen finden Sie unter Gewusst wie: Verwenden des Assistenten für Entity Data Model (Entity Framework) bzw. Gewusst wie: Manuelles Konfigurieren eines Entity Framework-Projekts und Gewusst wie: Manuelles Definieren eines Entity Data Model (Entity Framework).

Beispiel

Es folgt das LINQ to Entities -Beispiel.

Using context As New AdventureWorksEntities()

    Dim FirstName = "Frances"
    Dim LastName = "Adams"

    Dim contactQuery = From contact In context.Contacts _
        Where contact.LastName = LastName AndAlso contact.FirstName = FirstName _
            Select contact


    ' Iterate through the results of the parameterized query.
    For Each result In contactQuery
        Console.WriteLine("{0} {1}", result.FirstName, result.LastName)
    Next
End Using
using (AdventureWorksEntities context =
    new AdventureWorksEntities())
{
    string FirstName = "Frances";
    string LastName = "Adams";


    var contactQuery = from contact in context.Contacts
                       where contact.LastName == LastName && contact.FirstName == FirstName
                       select contact;

    // Iterate through the results of the parameterized query.
    foreach (var result in contactQuery)
    {
        Console.WriteLine("{0} {1} ", result.FirstName, result.LastName);
    }
}

Im Folgenden handelt es sich um das Entity SQL -Beispiel.

Using context As New AdventureWorksEntities()
    ' Create a query that takes two parameters. 
    Dim queryString As String = "SELECT VALUE Contact FROM AdventureWorksEntities.Contacts " & _
        " AS Contact WHERE Contact.LastName = @ln AND Contact.FirstName = @fn"

    Dim contactQuery As New ObjectQuery(Of Contact)(queryString, context)

    ' Add parameters to the collection. 
    contactQuery.Parameters.Add(New ObjectParameter("ln", "Adams"))
    contactQuery.Parameters.Add(New ObjectParameter("fn", "Frances"))

    ' Iterate through the collection of Contact items. 
    For Each result As Contact In contactQuery
        Console.WriteLine("Last Name: {0}; First Name: {1}", result.LastName, result.FirstName)
    Next
End Using
using (AdventureWorksEntities context =
    new AdventureWorksEntities())
{
    // Create a query that takes two parameters.
    string queryString =
        @"SELECT VALUE Contact FROM AdventureWorksEntities.Contacts 
                AS Contact WHERE Contact.LastName = @ln AND
                Contact.FirstName = @fn";

    ObjectQuery<Contact> contactQuery =
        new ObjectQuery<Contact>(queryString, context);

    // Add parameters to the collection.
    contactQuery.Parameters.Add(new ObjectParameter("ln", "Adams"));
    contactQuery.Parameters.Add(new ObjectParameter("fn", "Frances"));

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

Dies ist das Beispiel für die Abfrage-Generator-Methode.

Dim firstName As String = "Frances"
Dim lastName As String = "Adams"

Using context As New AdventureWorksEntities()
    ' Get the contacts with the specified name. 
    Dim contactQuery As ObjectQuery(Of Contact) = context.Contacts.Where("it.LastName = @ln AND it.FirstName = @fn", _
                                                     New ObjectParameter("ln", lastName), New ObjectParameter("fn", firstName))

    ' Iterate through the collection of Contact items. 
    For Each result As Contact In contactQuery
        Console.WriteLine("Last Name: {0}; First Name: {1}", result.LastName, result.FirstName)
    Next
End Using
string firstName = @"Frances";
string lastName = @"Adams";

using (AdventureWorksEntities context =
    new AdventureWorksEntities())
{
    // Get the contacts with the specified name.
    ObjectQuery<Contact> contactQuery = context.Contacts
        .Where("it.LastName = @ln AND it.FirstName = @fn",
        new ObjectParameter("ln", lastName),
        new ObjectParameter("fn", firstName));

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

Siehe auch

Aufgaben

Gewusst wie: Ausführen einer Abfrage, die Entitätstypobjekte zurückgibt (Entity Framework)
Gewusst wie: Ausführen einer Abfrage, die eine Auflistung von anonymen Typen zurückgibt (Entity Framework)
Gewusst wie: Ausführen einer Abfrage, die eine Auflistung von primitiven Typen zurückgibt (Entity Framework)

Konzepte

Entity SQL-Sprache