I’m new to EF and have read some articles. After reading I got confused in what is difference between lazy loading and eager loading?
- Can both type of queries be compiled?
- Can both type of queries return
IQuerableandIEnumerable? - Can both type of queries have Linq to Entities query syntex (select, from, where) and lambda expressions?
Kindly guide and help me.
Thanks a lot for your time and guidance
Only eager loading can be part of manually pre-compiled query. Lazy loading query is automatically created by EF and it is EF internal behavior if it actually pre-compiles it.
In eager loading you can control if the query returns
IQueryableorIEnumerable. Lazy loading query happens out of your control and you cannot modify it. If you want to useIQueryablefor lazily loaded navigation properties you must use third option called explicit loading where you getIQueryablequery for given navigation property and you can modify it.No. Neither of these queries have select, from, where. Lazy loading happens out of your control and eager loading doesn’t allow filtering – in both cases you always load all related objects.
Example of explicit query (the only type of loading where you can use query):