Ejemplos de sintaxis de consulta basada en métodos: Operadores de elementosMethod-Based Query Syntax Examples: Element Operators

Los ejemplos de este tema muestran cómo usar el First método para consultar el modelo AdventureWorks Sales utilizando sintaxis de consulta basada en métodos.The examples in this topic demonstrate how to use the First method to query the AdventureWorks Sales Model using method-based query syntax. El modelo AdventureWorks Sales que se usa en estos ejemplos se crea a partir de las tablas Contact, Address, Product, SalesOrderHeader y SalesOrderDetail en la base de datos de ejemplo de AdventureWorks.The AdventureWorks Sales Model used in these examples is built from the Contact, Address, Product, SalesOrderHeader, and SalesOrderDetail tables in the AdventureWorks sample database.

El ejemplo de este tema usa las siguientes using / Imports instrucciones:The example in this topic uses the following using/Imports statements:

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

FirstFirst

EjemploExample

En el ejemplo siguiente se usa el First método para buscar la primera dirección de correo electrónico que empieza por "caroline".The following example uses the First method to find the first email address that starts with 'caroline'.

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

Vea tambiénSee also