Question related to how ORM (Linq to SQL,EF 4) handles loading of data. Is the entire data set loaded, if so can we prevent the entire data set from loading and load a subset? For example when instantiating the context and querying it seems like there would be a huge performance issue if all data was returned (hundreds of thousand records) versus instantiating a connection, executing a proc for given subset of data and querying for this subset of data. I have not seen many threads address this, thanks in advance!
Share
No it doesn’t, as Linq to Sql/Eft will parse the query to produce an optimized sql query to retrive your data, with that being said a misuse of a extension method (which calls
GetEnumrator()) or query might end up loading all items from a Table.Misuse of extension methods that call
GetEnumerator()This will cause the whole
Bookstable to be loaded, since theToList()will invoke the sql query to retrive allBookobjects. From there youWhereandSelectare actually operating over Linq to objects and not Linq To Sql/Entities.Simply pushing your
.ToList()to the end of the statement will cause EF to parse the query into a valid sql query and retrieve a portion of your books when.ToList()is invoked.Misuse of
Func<T, bool>Consider the following,
Now this looks perfectly valid, however once you run the query you will find the context is loading all
Books, what gives? Simply put EF can not parse theFunc<T, bool>predicate to valid sql syntax since aFunc<>is just a delegate. This simple mistake can lead to EF loading all Books into the context and then running Linq to Objects against the loaded Books.Now here we are going to use an
Expression<Func<Book, bool>>which is simply an expression tree, EF knows how to parse an expression tree and is able to create a valid sql statement that only loads oneBookwith and Id of 3.I am sure other people will be able to chime in with a few more examples that can cause the whole Table to be loaded, however these are the most likely you will run into.