Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9156361
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T12:45:32+00:00 2026-06-17T12:45:32+00:00

How to replace Eager Loading by using Lazy loading ? Lets say like this.I

  • 0

How to replace Eager Loading by using Lazy loading ?

Lets say like this.I am having below kind of EF query with lot of Include keys .Performance wise This is very slow.

So how can I improve the performance of below code by using Lazy loading ?

from owner in Catalog.Owners
            where owner.Key == ownerKey
            from invoice in owner.Invoices
            where invoice.Provider.Key == providerKey
            where invoice.Id == id
            select invoice)
            .Include(i => i.Owner.Credits)
            .Include(i => i.Provider)
            .Include(i => i.Items.Select(s => s.Allocation.Service))
            .Include(i => i.Items.Select(s => s.Allocation.Pet))
            .FirstOrDefault();

If you can give me a sample code explanation it’s perfect.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T12:45:34+00:00Added an answer on June 17, 2026 at 12:45 pm

    With EF 4.1 and greater, you just need to make sure to define your code-first navigation properties (properties that link to other entities) as virtual. Then, if you are inside a DbContext, the linked entity will only be queried from the database when it is actually used (lazy loaded).

    However, eager loading is used when you want all the data right away. There are usually good reasons for this. So, I would first say that if you don’t know why you are eager loading, then don’t do it. Make sure your properties that link entities are virtual and then get rid of the .Include statements in your query.

    However, if you are taking the data you are querying outside of a DbContext or you use the data right away it makes sense to pull that data into memory for the duration of your operation.

    So, in response to your question, it is not always a case of just replacing eager loading with lazy loading. You must look at what is being done with the data once it is queried and see how to only return the minimum set you need.

    Added example:

        public class Person
        {
            public int Id { get; set; }
            public string Name { get; set; }
    
            public virtual ICollection<Car> Cars { get; set; } //make it virtual
    
        }
    
        public class Car
        {
            public int Id { get; set; }
            public string Make { get; set; }
            public string Model { get; set; }
        }
    
        public class MyContext : DbContext
        {
            public IDbSet<Person> People { get; set; }
            public IDbSet<Car> Cars { get; set; }
        }
    
        class Program
        {
            private static void Main(string[] args)
            {
                using (var context = new MyContext())
                {
                    var person = context.People.FirstOrDefault(p => p.Id == 1); // calls the database
    
                    var car = person.Cars.FirstOrDefault(c => c.Id == 2); // Lazy loads Cars
    
                }
            }
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Replace # with \u0023 in a Java String which looks like below: {subjectCategory:s123, subjectId:111222333,
input.replace(&amp;, &).replace(&lt;, <).replace(&gt;, >).replace(&quot;, \); im using this line of code as a sudo
How can we replace symbols from string in C#? Like this Input : �Click
This method works great and generates only 1 query (utilizing .Includes() to eager load).
replace newline seems to be a question asked here and there like hundred times
How can I replace caracters on the fly when using sed ? Example :
This is working newRow.replace('[[' + key + ']]', item); but i tried replacing with
using Replace on the string clientNameStr causes an Object Reference Not Found error. //
JavaScript replace() Method Hello username !Don’t forget me this weekend! Username . When i
Goal Replace one div with another after 5 seconds delay using jQuery. Description I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.