共用方式為


查詢結果

LINQ 到實體 查詢轉換成命令樹並且執行之後,查詢結果通常會傳回成為下列其中一種形式:

  • 零個或多個具型別實體物件的集合或是 EDM 中複雜類型的投影。

  • EDM 所支援的 CLR 型別。

  • 內嵌集合。

  • 匿名型別。

針對資料來源執行查詢之後,便會將結果具體化成為 CLR 型別,然後傳回用戶端。所有物件具體化都是由 實體架構 執行。因為無法在 實體架構 與 CLR 之間對應而產生的任何錯誤,都將導致物件具體化期間擲回例外狀況。

如果查詢執行傳回基本 EDM 型別,結果將由獨立且從 實體架構 中斷連接的 CLR 型別構成。但是,如果查詢傳回以 ObjectQuery 表示的具型別實體物件集合,這些型別將會由此物件內容追蹤。所有物件行為 (例如子/父集合、變更追蹤、多型等) 都會如 實體架構 中所定義。這項功能可以依照 實體架構 中所定義的能力使用。如需詳細資訊,請參閱物件服務概觀 (Entity Framework)

從查詢傳回的結構型別 (例如匿名型別和可為 null 的複雜類型) 可能是 null 值。所傳回實體的 EntityCollection 屬性也可能是 null 值。投影 null 值之實體的屬性集合就可能產生這種情況,例如在沒有項目的 ObjectQuery 上呼叫 FirstOrDefault

在某些情況下,查詢看起來好像會在執行期間產生具體化結果,但是查詢是要在伺服器上執行,所以實體物件永遠也不會在 CLR 中具體化。如果您需要用到物件具體化的副作用,這種情況將會造成問題。

以下範例包含具有 LastName 屬性的自訂類別 MyContact。當 LastName 屬性設定之後,count 變數就會累加。如果您執行以下兩個查詢,第一個查詢會累加 count,但第二個查詢則不會。這是因為在第二個查詢中 LastName 屬性是從結果投影的,而且根本沒有建立 MyContact 類別,因為在存放區上執行此查詢並不要用到它。

Public count As Integer = 0

Sub Main()

    Using AWEntities As New AdventureWorksEntities()

        Dim query1 = AWEntities.Contact _
        .Where(Function(c) c.LastName = "Jones") _
        .Select(Function(c) New MyContact With {.LastName = c.LastName})

        ' Execute the first query and print the count.
        query1.ToList()
        Console.WriteLine("Count: " & count)

        ' Reset the count variable.
        count = 0

        Dim query2 = AWEntities _
        .Contact() _
        .Where(Function(c) c.LastName = "Jones") _
        .Select(Function(c) New MyContact With {.LastName = c.LastName}) _
        .Select(Function(x) x.LastName)

        ' Execute the second query and print the count.
        query2.ToList()
        Console.WriteLine("Count: " & count)

    End Using
End Sub
public static int count = 0;

static void Main(string[] args)
{
    using (AdventureWorksEntities AWEntities = new AdventureWorksEntities())
    {

        var query1 = AWEntities
           .Contact
           .Where(c => c.LastName == "Jones")
           .Select(c => new MyContact { LastName = c.LastName });

        // Execute the first query and print the count.
        query1.ToList();
        Console.WriteLine("Count: " + count);

        //Reset the count variable.
        count = 0;

        var query2 = AWEntities
           .Contact
           .Where(c => c.LastName == "Jones")
           .Select(c => new MyContact { LastName = c.LastName })
           .Select(my => my.LastName);

        // Execute the second query and print the count.
        query2.ToList();
        Console.WriteLine("Count: " + count);

    }

    Console.WriteLine("Hit enter...");
    Console.Read();
}
Public Class MyContact

    Private _lastName As String

    Public Property LastName() As String
        Get
            Return _lastName
        End Get

        Set(ByVal value As String)
            _lastName = value
            count += 1
        End Set
    End Property

End Class
public class MyContact
{

    String _lastName;

    public string LastName
    {
        get
        {
            return _lastName;
        }

        set
        {
            _lastName = value;
            count++;
        }
    }
}

另請參閱

其他資源

用 LINQ to Entities 查詢