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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T16:13:11+00:00 2026-05-29T16:13:11+00:00

I am using Entity Framework CodeFirst where I have used Parent Child relations using

  • 0

I am using Entity Framework CodeFirst where I have used Parent Child relations using ICollection as

public class Person
{
   public string UserName { get;set}
   public ICollection<Blog> Blogs { get; set;}
}

public class Blog
{
   public int id { get; set; }
   public string Subject { get; set; }
   public string Body { get; set; }
}

Ok, so far everything is working ok, but my concern is, whenever I want to get the Blogs of a person, I get it as

var thePerson = _context.Persons.Where(x => x.UserName = 'xxx').SingleOrDefault();
var theBlogs = thePerson.Blogs.OrderBy(id).Take(5);

Now, I understand that, when the line is executed, all Blogs for that person is loaded into the memory and then sorting and selecting is done from memory. That is not ideal for a record of Person who has large number of blogs. I want to make the Blog Child as IQueryable so that the Sorting and Selecting is done in SQL database before pulling to Memory.

I know I could declare the Blogs as IQueryable in my context so that I could directly query as

var theBlogs = _context.Blogs.Where(.....)

but that is not feasible for me due to design choice, I want to avoid any circular reference as much as possible due to serialization problem. So, I did not make any reference of the parent entity in my child.

I found that, i can call AsQueryable() method on the blogs as

var theBlogs = thePerson.Blogs.AsQueryable().OrderBy(id).Take(5);

That looks like a magic for me and seems too good to be true. So my question. Does this AsQueryable really make the ICollection as IQueryable in reality and makes all Query process in SQL Server (Lazy loading) OR it is just a casting where Blogs are loaded into memory as like before, but change the interface from ICollection to IQueryable ?

  • 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-05-29T16:13:12+00:00Added an answer on May 29, 2026 at 4:13 pm

    So actually it appears that writing your navigation property as IQueryable<T> is not possible.

    What you could do is adding a navigation property to Blog:

    public class Blog
    {
       public int id { get; set; }
       public string Subject { get; set; }
       public string Body { get; set; }
       public virtual Person Owner { get; set; }
    }
    

    From that, you can query as follows so it won’t load everything into memory:

    var thePerson = _context.Persons.Where(x => x.UserName = 'xxx').SingleOrDefault();
    var results = _context.Blogs.Where(z => z.Person.Name = thePerson.Name).OrderBy(id).Take(5)
    

    I suggest you to try LINQPad to see how LINQ is translated into SQL, and what is actually requested from the DB.

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

Sidebar

Related Questions

I'm using ASP.NET MVC 3 with Entity Framework CodeFirst I have two classes as
I am using Entity Framework with my website. To improve performance, I have started
I'm using Entity Framework in my project, and I have the problem that, once
I have a problem with the following Linq query using Entity Framework: from o
I have an ASP.NET Dynamic Data application (using Entity Framework) in which I have
I have been using Entity Framework CTP with Code-First as in this tutorial by
Background We have a WinForms application with Entity Framework 4.2 code-first / FluentAPI using
I have a small MVC 3 app using Entity Framework Code First and use
I have never used Entity Framework in a project before so I am not
Using the Entity Framework Code First paradigm I have defined the following objects and

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.