HOW TO:執行傳回實體類型物件的查詢 (Entity Framework)

本主題提供的範例將說明如何執行查詢來傳回實體的集合。 然後,它會逐一查看 Contacts 的集合,並顯示每一項 Contact 的名字和姓氏。 . 若只要傳回單一物件,請在查詢頂端使用下列其中一個方法:FirstFirstOrDefaultSingleSingleOrDefault

相同範例會使用下列每個 Entity Framework 查詢技術顯示:

  • LINQ to Entities

  • Entity SQL 與 ObjectQuery<T>

  • ObjectQuery<T> 的查詢產生器方法

本主題的範例根據 Adventure Works Sales Model。若要執行此主題中的程式碼,您必須已經將 Adventure Works Sales Model 加入到專案中,並設定您的專案使用 Entity Framework。如需詳細資訊,請參閱 HOW TO:使用實體資料模型精靈 (Entity Framework)HOW TO:手動設定 Entity Framework 專案HOW TO:手動設定 Entity Framework 專案

範例

以下是 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);
}

另請參閱

工作

HOW TO:執行傳回匿名型別集合的查詢 (Entity Framework)
HOW TO:執行傳回基本型別集合的查詢
HOW TO:執行參數化查詢 (Entity Framework)

其他資源

定義進階資料模型 (Entity Framework 工作)