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

In diesem Thema wird anhand von Beispielen gezeigt, wie Abfragen ausgeführt werden, die eine Auflistung von primitiven Typen zurückgeben. Verwenden Sie eine der folgenden Methoden über der Abfrage, um nur ein einzelnes Objekt zurückzugeben: First, FirstOrDefault, Single, SingleOrDefault.

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

Im Folgenden handelt es sich um das LINQ to Entities -Beispiel.

Dim contactId As Integer = 377

Using context As New AdventureWorksEntities()
    ' Select a value. 
    Dim orders As ObjectSet(Of SalesOrderHeader) = context.SalesOrderHeaders

    Dim orderQuery As IQueryable(Of Int32) = From order In orders _
        Where order.Contact.ContactID = contactId _
        Select order.PurchaseOrderNumber.Length

    ' Iterate through the collection of values. 
    For Each result As Int32 In orderQuery
        Console.WriteLine("{0}", result)
    Next

    ' Use a nullable DateTime value because ShipDate can be null. 
    Dim shipDateQuery As IQueryable(Of System.Nullable(Of DateTime)) = From order In orders _
        Where order.Contact.ContactID = contactId _
        Select order.ShipDate

    ' Iterate through the collection of values. 
    For Each shipDate As System.Nullable(Of DateTime) In shipDateQuery
        Dim shipDateMessage As String = "date not set"

        If shipDate IsNot Nothing Then
            shipDateMessage = shipDate.ToString()
        End If
        Console.WriteLine("Ship Date: {0}.", shipDateMessage)
    Next
End Using
int contactId = 377;

using (AdventureWorksEntities context
    = new AdventureWorksEntities())
{
    // Select a value.
    ObjectSet<SalesOrderHeader> orders
        = context.SalesOrderHeaders;

    IQueryable<Int32> orderQuery =
        from order in orders
        where order.Contact.ContactID == contactId
        select order.PurchaseOrderNumber.Length;

    // Iterate through the collection of values.
    foreach (Int32 result in orderQuery)
    {
        Console.WriteLine("{0}", result);
    }

    // Use a nullable DateTime value because ShipDate can be null.
    IQueryable<DateTime?> shipDateQuery =
        from order in orders
        where order.Contact.ContactID == contactId
        select order.ShipDate;

    // Iterate through the collection of values.
    foreach (DateTime? shipDate in shipDateQuery)
    {
        string shipDateMessage = "date not set";

        if (shipDate != null)
        {
            shipDateMessage = shipDate.ToString();
        }
        Console.WriteLine("Ship Date: {0}.", shipDateMessage);
    }
}

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

Dim contactId As Integer = 377

Using context As New AdventureWorksEntities()
    Dim orderQueryString As String = "SELECT VALUE Length(order.PurchaseOrderNumber) FROM " & _
        " AdventureWorksEntities.SalesOrderHeaders AS order WHERE order.CustomerID = @contactId"
    Dim shipDateQueryString As String = "SELECT VALUE order.ShipDate" & _
        " FROM AdventureWorksEntities.SalesOrderHeaders AS order WHERE order.CustomerID = @contactId"

    ' Use the SelectValue method to select a value. 
    Dim orderQuery As New ObjectQuery(Of Int32)(orderQueryString, context, MergeOption.NoTracking)
    orderQuery.Parameters.Add(New ObjectParameter("contactId", contactId))

    ' Iterate through the collection of values. 
    For Each result As Int32 In orderQuery
        Console.WriteLine("{0}", result)
    Next

    ' Use a nullable DateTime value because ShipDate can be null. 
    Dim shipDateQuery As New ObjectQuery(Of Nullable(Of DateTime))(shipDateQueryString, context, MergeOption.NoTracking)
    shipDateQuery.Parameters.Add(New ObjectParameter("contactId", contactId))

    ' Iterate through the collection of values. 
    For Each shipDate As Nullable(Of DateTime) In shipDateQuery
        Dim shipDateMessage As String = "date not set"

        If shipDate IsNot Nothing Then
            shipDateMessage = shipDate.ToString()
        End If
        Console.WriteLine("Ship Date: {0}.", shipDateMessage)
    Next
End Using
int contactId = 377;

using (AdventureWorksEntities context =
    new AdventureWorksEntities())
{
    string orderQueryString = @"SELECT VALUE Length(order.PurchaseOrderNumber)
        FROM AdventureWorksEntities.SalesOrderHeaders AS order
        WHERE order.CustomerID = @contactId";
    string shipDateQueryString = @"SELECT VALUE order.ShipDate
        FROM AdventureWorksEntities.SalesOrderHeaders AS order
        WHERE order.CustomerID = @contactId";

    // Use the SelectValue method to select a value.
    ObjectQuery<Int32> orderQuery =
        new ObjectQuery<Int32>(orderQueryString,
            context, MergeOption.NoTracking);
    orderQuery.Parameters.Add(
        new ObjectParameter("contactId", contactId));

    // Iterate through the collection of values.
    foreach (Int32 result in orderQuery)
    {
        Console.WriteLine("{0}", result);
    }

    // Use a nullable DateTime value because ShipDate can be null.
    ObjectQuery<Nullable<DateTime>> shipDateQuery =
        new ObjectQuery<Nullable<DateTime>>(shipDateQueryString,
    context, MergeOption.NoTracking);
    shipDateQuery.Parameters.Add(
        new ObjectParameter("contactId", contactId));

    // Iterate through the collection of values.
    foreach (Nullable<DateTime> shipDate in shipDateQuery)
    {
        string shipDateMessage = "date not set";

        if (shipDate != null)
        {
            shipDateMessage = shipDate.ToString();
        }
        Console.WriteLine("Ship Date: {0}.", shipDateMessage);
    }
}

Im Folgenden wird ein Beispiel für die Abfrage-Generator-Methode dargestellt.

Dim contactId As Integer = 377

Using context As New AdventureWorksEntities()
    ' Use the SelectValue method to select a value. 
    Dim orderQuery As ObjectQuery(Of Int32) = context.SalesOrderHeaders.Where("it.CustomerID = @contactId", _
                        New ObjectParameter("contactId", contactId)).SelectValue(Of Int32)("Length(it.PurchaseOrderNumber)")

    ' Iterate through the collection of values. 
    For Each result As Int32 In orderQuery
        Console.WriteLine("{0}", result)
    Next

    ' Use a nullable DateTime value because ShipDate can be null. 
    Dim shipDateQuery As ObjectQuery(Of Nullable(Of DateTime)) = _
        context.SalesOrderHeaders.Where("it.CustomerID = @contactId", _
            New ObjectParameter("contactId", contactId)).SelectValue(Of Nullable(Of DateTime))("it.ShipDate")

    ' Iterate through the collection of values. 
    For Each shipDate As Nullable(Of DateTime) In shipDateQuery
        Dim shipDateMessage As String = "date not set"

        If shipDate IsNot Nothing Then
            shipDateMessage = shipDate.ToString()
        End If
        Console.WriteLine("Ship Date: {0}.", shipDateMessage)
    Next
End Using
int contactId = 377;

using (AdventureWorksEntities context =
    new AdventureWorksEntities())
{
    // Use the SelectValue method to select a value.
    ObjectQuery<Int32> orderQuery =
        context.SalesOrderHeaders
        .Where("it.CustomerID = @contactId",
        new ObjectParameter("contactId", contactId))
        .SelectValue<Int32>("Length(it.PurchaseOrderNumber)");

    // Iterate through the collection of values.
    foreach (Int32 result in orderQuery)
    {
        Console.WriteLine("{0}", result);
    }

    // Use a nullable DateTime value because ShipDate can be null.
    ObjectQuery<Nullable<DateTime>> shipDateQuery =
        context.SalesOrderHeaders
        .Where("it.CustomerID = @contactId",
            new ObjectParameter("contactId", contactId))
        .SelectValue<Nullable<DateTime>>("it.ShipDate");

    // Iterate through the collection of values.
    foreach (Nullable<DateTime> shipDate in shipDateQuery)
    {
        string shipDateMessage = "date not set";

        if (shipDate != null)
        {
            shipDateMessage = shipDate.ToString();
        }
        Console.WriteLine("Ship Date: {0}.", shipDateMessage);
    }
}

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 parametrisierten Abfrage (Entity Framework)

Konzepte

Abfrage-Generator-Methoden (Entity Framework)