views:

52

answers:

1

the use case that I'm concerned with in this post involves iterating over a large number of entities (100K+) returned from a query.

Given the following code snippet:

var query = from c in context.Customers select c;
foreach(var customer in query)
    printCustomerStatement(customer);

In this example it's clear that the the customer instance is not needed after the call to printCustomerStatement. Will the ObjectContext be keeping a reference to it regardless? My expectation is that it would not. and that this foreach would behave like a forward-only read-only enumerator method call.

+2  A: 

Depends on the query MergeOption.

If you do this:

context.Customers.MergeOption = MergeOption.NoTracking;
var query = from c in context.Customers select c;
foreach(var customer in query)
    printCustomerStatement(customer);

...then the context won't store those references.

With the default MergeOption of AppendOnly, it will.

Craig Stuntz