基于方法的查询语法示例:元素运算符
本主题中的示例演示如何使用 First 方法通过基于方法的查询语法来查询 AdventureWorks 销售模型。 这些示例中使用的 AdventureWorks 销售模型从 AdventureWorks 示例数据库中的 Contact、Address、Product、SalesOrderHeader 和 SalesOrderDetail 等表生成。
本主题中的示例使用下面的 using
/Imports
语句:
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
示例
以下示例使用 First 方法查找以“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