Beispiele für die methodenbasierte Abfragesyntax: Elementoperatoren

In diesem Thema wird anhand von Beispielen gezeigt, wie Sie mithilfe der First-Methode und der methodenbasierten Abfragesyntax das AdventureWorks Sales-Modell abfragen können. Für das in den Beispielen verwendete "AdventureWorks Sales"-Modell wurde auf die Tabellen Contact, Address, Product, SalesOrderHeader und SalesOrderDetail der "AdventureWorks"-Beispieldatenbank zurückgegriffen.

Das Beispiel in diesem Thema verwendet die folgenden using/Imports-Anweisungen:

using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Objects;
using System.Globalization;
using System.Data.EntityClient;
using System.Data.SqlClient;
using System.Data.Common;

Option Explicit On
Option Strict On
Imports System.Data.Objects
Imports System.Globalization

First

Beispiel

Im folgenden Beispiel wird die First-Methode verwendet, um die erste E-Mail-Adresse zu suchen, die mit "caroline" beginnt.

string name = "caroline";
using (AdventureWorksEntities context = new AdventureWorksEntities())
{
    ObjectSet<Contact> contacts = context.Contacts;

    Contact query = contacts.First(contact =>
        contact.EmailAddress.StartsWith(name));

    Console.WriteLine("An email address starting with 'caroline': {0}",
        query.EmailAddress);
}
Dim name = "caroline"
Using context As New AdventureWorksEntities
    Dim contacts As ObjectSet(Of Contact) = context.Contacts

    Dim query = contacts.First(Function(cont) _
            cont.EmailAddress.StartsWith(name))

    Console.WriteLine("An email address starting with 'caroline': {0}", _
            query.EmailAddress)
End Using

Siehe auch