如何:执行返回实体类型对象的查询(实体框架)

本主题提供有关如何执行返回实体集合的查询的示例。 此示例循环访问 Contacts 集合并显示每个 Contact 的名字和姓氏。 . 若要只返回单个对象,请对您的查询使用下列方法之一:FirstFirstOrDefaultSingleSingleOrDefault

将使用以下每种实体框架 查询技术演示同一示例:

  • LINQ to Entities

  • Entity SQL 以及 ObjectQuery<T>

  • ObjectQuery<T> 的查询生成器方法

本主题中的示例基于 Adventure Works 销售模型。若要运行本主题中的代码,则必须已经将 Adventure Works 销售模型添加到了您的项目中,并且已经将项目配置为使用实体框架。有关更多信息,请参见如何:使用实体数据模型向导(实体框架)如何:手动配置实体框架项目如何:手动定义实体数据模型(实体框架)

示例

以下是 LINQ to Entities 示例。

Using context As New AdventureWorksEntities()
    Dim LastName = "Zhou"
    Dim query = From contact In context.Contacts Where contact.LastName = LastName

    ' Iterate through the collection of Contact items.
    For Each result As Contact In query
        Console.WriteLine("Contact First Name: {0}; Last Name: {1}", _
                result.FirstName, result.LastName)
    Next
End Using
using (AdventureWorksEntities context = new AdventureWorksEntities())
{
    string LastName = "Zhou";
    var query = from contact in context.Contacts where 
                    contact.LastName == LastName select contact;

    // Iterate through the collection of Contact items.
    foreach( var result in query)
    {
        Console.WriteLine("Contact First Name: {0}; Last Name: {1}", 
                result.FirstName, result.LastName);
    }
}

以下是 Entity SQL 示例。

Using context As New AdventureWorksEntities()

    Dim esqlString As String = "SELECT VALUE Contact " & _
        "FROM AdventureWorksEntities.Contacts as Contact where Contact.LastName = @ln"

    Dim query As New ObjectQuery(Of Contact)(esqlString, context, MergeOption.NoTracking)
    query.Parameters.Add(New ObjectParameter("ln", "Zhou"))

    ' Iterate through the collection of Contact items. 
    For Each result As Contact In query
        Console.WriteLine("Contact First Name: {0}; Last Name: {1}", _
                result.FirstName, result.LastName)
    Next
End Using
using (AdventureWorksEntities context =
    new AdventureWorksEntities())
{
    string esqlQuery = @"SELECT VALUE Contact
        FROM AdventureWorksEntities.Contacts as Contact where Contact.LastName = @ln";

    // The following query returns a collection of Contact objects.
    ObjectQuery<Contact> query = new ObjectQuery<Contact>(esqlQuery, context, MergeOption.NoTracking);
    query.Parameters.Add(new ObjectParameter("ln", "Zhou"));

    // Iterate through the collection of Contact items.
    foreach (Contact result in query)
        Console.WriteLine("Contact First Name: {0}; Last Name: {1}",
                result.FirstName, result.LastName);
}

以下是查询生成器方法示例。

Using context As New AdventureWorksEntities()
    Dim query As ObjectQuery(Of Contact) = _
        context.Contacts.Where("it.LastName==@ln",
        New ObjectParameter("ln", "Zhou"))

    ' Iterate through the collection of Contact items.
    For Each result As Contact In query
        Console.WriteLine("Contact First Name: {0}; Last Name: {1}", _
                result.FirstName, result.LastName)
    Next
End Using
using (AdventureWorksEntities context =
    new AdventureWorksEntities())
{
    ObjectQuery<Contact> query= context.Contacts.Where("it.LastName==@ln", 
        new ObjectParameter("ln", "Zhou"));

    // Iterate through the collection of Contact items.
    foreach (Contact result in query)
        Console.WriteLine("Contact First Name: {0}; Last Name: {1}",
                result.FirstName, result.LastName);
}

另请参见

任务

如何:执行返回匿名类型集合的查询(实体框架)
如何:执行返回基元类型集合的查询(实体框架)
如何:执行参数化查询(实体框架)

其他资源

定义高级数据模型(实体框架任务)