隐藏

如何使用 First 方法通过使用基于方法的查询语法来查询-元素运算符

发布:2021/7/9 16:59:51作者:管理员 来源:本站 浏览次数:921

本主题中的示例演示如何使用 First 方法通过使用基于方法的查询语法来查询 AdventureWorks 销售模型 。 这些示例中使用的 AdventureWorks 销售模型从 AdventureWorks 示例数据库中的 Contact、Address、Product、SalesOrderHeader 和 SalesOrderDetail 等表生成。


本主题中的示例使用以下 using / Imports 语句:

C#


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;


First

示例


下面的示例使用 First 方法查找以 "caroline" 开头的第一个电子邮件地址。

C#


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);

}


请参阅


   LINQ to Entities 中的查询