Lazy loading and Deferred are pretty synonymous (AFAIK, please correct me if I am wrong). The big difference between Eager and Lazy. The wait will come in front, Lazy will only happen “as needed”, and execution will be done at the DB level - let’s take a simple JOIN as an example
var people = (from p in people SELECT p).ToList(); var jobs = (from j in jobs SELECT j).ToList(); var peopleAndJobs = (from p in people JOIN j on j.personId equals p.personId SELECT p).ToList()
This is an example of heavy loading. We get ALL people, ALL work, and we do the unification in memory. Not very smart (usually). This is what the Lazy style looks like.
var people = (from p in people SELECT p); var jobs = (from j in jobs SELECT j); var peopleAndJobs = (from p in people JOIN j on j.personId equals p.personId SELECT p).ToList()
This makes the creation of IQueryable for people and for work (IQueryable is lazy), and the connection occurs in the database. This saves network activity and usually works faster because the database is optimized for consolidation, etc.
If we don’t say directly: “I need this data!” (by ToList it, iterating through it, etc.), he is lazy. There are a few other quirks, but this should be a decent tutorial.
Mike M.
source share