I’m working with entity framework with POCO objects and I’ve got LazyLoading turned on.
If i’m working with a collection associated with an object, when is the collection fully loaded and in what circumstances?
If I call each of these
Order.OrderItems.Count()
Order.OrderItems.Any(x => x.StatusId = aValue)
Order.OrderItems.All(x => x.StatusId = aValue)
Do any of these guarantee the complete loading of the OrderItems collection?
At points in the code we are calling
Order.Include(“OrderItems”)
when querying or
context.LoadProperty(order, “OrderItems”)
after querying
But I’ve realised sometimes this isn’t always happening – and I want to know the consequences of this not occurring. I think I’ve got a bit of a knowledge gap with it
Linq methods don’t generally load your data until you either
foreachover their result, or append aToList,ToArray, etc. This is how the Linq to Entities provider can allow you to chain methods and only build a query over the final structure.If a method returns an
IQueryableorIEnumerable, you can be pretty sure that Linq won’t load the data at that point.But:
For an outer-most query, if Linq must return a boolean value or an integer then it must run your query immediately. But things get a little more complicated for nested queries, since they won’t be evaluated until the outer query gets evaluated.
As for:
I believe an
Includewon’t resolve a query on its own. You use it to piggyback additional data when you’re making another query.Linq in general
If you want to know (generally) how Linq works, you can check out this set of articles:
http://msmvps.com/blogs/jon_skeet/archive/tags/Edulinq/default.aspx
That will give you an idea of when an
IEnumerablecan be deferred, and when it must be evaluated. It won’t tell you everything about the Linq to Entities provider, but a good portion of the knowledge will transfer.