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 8506633
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T02:38:37+00:00 2026-06-11T02:38:37+00:00

I am going to make this REALLY simple. I have two tables. public class

  • 0

I am going to make this REALLY simple.
I have two tables.

public class User
{
    public virtual int UserId { get; set; } //primary key AND foreign key
    //user attributes in here
    public virtual UserProfile UserProfile { get; set; }
}
public class UserProfile
{
    public virtual int UserId { get; set; } //primary key AND foreign key
    //profile attributes in here
    public virtual User User { get; set; }
}

Basically they are two tables that share a primary key in a 1-1 relationship. Whether these should be combined into one or not, I don’t know, I am basing this off of an existing database.

Now, the issue I have is when I access it.

THIS CODE GOES FAST(second, maybe two):

List<User> userList; //**This userList is defined elsewhere, but it's a list of about 400 users.
foreach (User user in userList)
{
    var test = user.FirstName;
}

THIS CODE GOES REALLY SLOW(10-30 seconds):

List<User> userList; //**This userList is defined elsewhere, but it's a list of about 400 users.
foreach (User user in userList)
{
    var test = user.UserProfile.EmailAddress;
}

Why does my code take so much longer when I access the UserProfile from the user table?!

  • 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-11T02:38:39+00:00Added an answer on June 11, 2026 at 2:38 am

    Probably because you are lazy loading UserProfile here. This means that for each iteration in your loop you make an individual call to the DB to load UserProfile when you attempt to access the email address.

    I’m not sure how you are creating the userList collection, but assuming you are doing a simple query over your User table you can use Include to eager load any properties that you would like to have up front:

    var userList = (from u in dbContext.Users.Include("UserProfile")
                    select u)
    

    Now the performance hit is limited to when this query is intially executed, enumerating over the results would no longer require individual calls to the DB.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is probably going to be a really simple explanation, but I'm going to
I've got a feeling this is going to be something really simple, but anyway.
There is probably a really simple answer for this ... I have a project
I am trying to make a really simple Tic-Tac-Toe game. I have stored values
To make this more clear, I'm going to put code samples: $file = fopen('filename.ext',
Ok, I'm going to try to make this more clear because my last question
Im going to make simple game with fallowing architecture: Initialization OpenWindowAndGraphics while(game_not_end) { ReadEvents
I am going to make a GUI that will have dynamically created sets of
I wanna make this rather simple to ask so I can hope for a
I have a really simple e-commerce in my website. It's just a shopping cart,

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.