What is difference between eager loading and Lazy Loading?

Published by Charlie Davidson on

What is difference between eager loading and Lazy Loading?

Lazy Loading vs. Eager Loading. While lazy loading delays the initialization of a resource, eager loading initializes or loads a resource as soon as the code is executed. Eager loading also involves pre-loading related entities referenced by a resource.

Is IEnumerable Lazy Loading?

IEnumerable doesn’t support lazy loading. IQueryable support lazy loading. Hence it is suitable for paging like scenarios.

What’s the difference between returning IEnumerable and IQueryable?

Returning an IEnumerable will automatically force the runtime to use LINQ to Objects to query your collection. Returning an IQueryable (which implements IEnumerable, by the way) provides the extra functionality to translate your query into something that might perform better on the underlying source (LINQ to SQL, LINQ to XML, etc.).

How does misuse of IEnumerable < T > impact LINQ query performance?

There is a blog post with brief source code sample about how misuse of IEnumerable can dramatically impact LINQ query performance: Entity Framework: IQueryable vs. IEnumerable. If we dig deeper and look into the sources, we can see that there are obviously different extension methods are perfomed for IEnumerable :

What’s the difference between Entity Framework and IQueryable?

The intent of IQueryable is mainly to provide data providers with standard contracts to write query implementation against their data source. From the above definition, it is clear that data providers like Entity Framework Core implements IQueryable. To show how it works, I am going to use Entity Framework Core to connect to an existing database.

What is the purpose of the IEnumerable interface?

The main purpose of the IEnumerable interface is to expose an enumerator. And the enumerator is for iterating through a collection or an array. To illustrate this further, let me first create a new .Net Core Console application. I will open Visual Studio 2019, and in Create a new project popup I will select Console App (.NET Core).

Categories: Trending