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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T17:48:22+00:00 2026-05-17T17:48:22+00:00

Note: I know there are a number of questions around for issues with Linq’s

  • 0

Note: I know there are a number of questions around for issues with Linq’s .Include(table) not loading data, I believe I have exhausted the options people have listed, and still had problems.

I have a large Linq2Entities query on an application I’m maintaining. The query is built up as such:

IQueryable<Results> query = context.MyTable
    .Where(r =>
    r.RelatedTable.ID == 2 &&
    r.AnotherRelatedTable.ID == someId);

Then predicates are built up depending on various business logic, such as:

if (sortColumn.Contains("dob "))
{
    if (orderByAscending)
        query = query.OrderBy(p => p.RelatedTable.OrderByDescending(q => q.ID).FirstOrDefault().FieldName);
    else
        query = query.OrderByDescending(p => p.RelatedTable.OrderByDescending(q => q.ID).FirstOrDefault().FieldName);
}

Note – there is always a sort order provided.

Originally the included tables were set at the beginning, after reading articles such as the famous Tip 22, so now they are done at the end (which didn’t fix the problem):

var resultsList = (query.Select(r => r) as ObjectQuery<Results>)
    .Include("RelatedTable")
    .Include("AnotherRelatedTable")
    .Skip((page - 1) * rowsPerPage)
    .Take(rowsPerPage);

Seemingly at random (approximately for every 5000 users of the site, this issue happens once) the RelatedTable data won’t load. It can be brute forced by calling load on the related table. But even the failure to load isn’t consistent, I’ve run the query in testing and it’s worked, but most of the time hasn’t, without changing any code or data.

It is fine, when the skip and take aren’t included, and the whole dataset is returned, but I would expect the skip and take to be done on the complete dataset – it certainly appears to be from profiling the SQL…

UPDATE 16/11/10: I have profiled the SQL against a problem data set, and I’ve been able to reproduce the query failing about 9/10 times, but succeeding the rest. The SQL being executed is identical when the query fails or succeeds except, as expected, for the parameters passed to the SQL.

The issue has been solved with the following change, but the question remains as to why this should be.

Failing – get LINQ to handle the rows:

var resultsList = (query.Select(r => r) as ObjectQuery<Results>)
    .Include("RelatedTable")
    .Include("AnotherRelatedTable")
    .Skip((page - 1) * rowsPerPage)
    .Take(rowsPerPage)
    .ToList();

Working – enumerate the data then get the rows:

var resultsList = (query.Select(r => r) as ObjectQuery<Results>)
    .Include("RelatedTable")
    .Include("AnotherRelatedTable")
    .ToList()
    .Skip((page - 1) * rowsPerPage)
    .Take(rowsPerPage);

Unfortunately the SQL this query creates contains some sensitive schema data so I can’t post it, it is also 1400 lines long, so I wouldn’t subject the public to it anyway!

  • 1 1 Answer
  • 1 View
  • 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-17T17:48:23+00:00Added an answer on May 17, 2026 at 5:48 pm

    The sole effect of Take() is to change the generated SQL. Other than that, the Entity Framework does not care about it at all. Same for .Skip(). It’s hard to believe that this would have an effect on query materialization (although stranger things have happened).

    So what could be causing this behavior? Off the top of my head:

    1. A bug in your application or mapping which is causing an incorrect query to be generated.
    2. A bug in the Entity Framework which would cause returned data to be materialized into objects incorrectly in certain circumstances.
    3. Bad data in your database.
    4. A bug in your database’s SQL parser.

    I don’t think you’re going to get a lot further with this until you can capture the generated SQL and run it yourself. This is actually not terribly hard, as you can set up a SQL profiler with an appropriate filter. If you find that the generated SQL is different in the buggy case, you can work backwards from there. If you find that the generated SQL is identical in the buggy case, the next step would be to look at the rows returned, preferably in the same context as the application ran it.

    In short, I think you just have to keep tweaking your SQL profiling until you have the information you need.

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

Sidebar

Related Questions

NOTE: I know there are many questions that talked about that but I'm still
Please note that I'm not asking how but why. And I don't know if
Note: this is NOT about concurrency. This is about the thread macro. I know
I know there are a couple of other questions on here with the exact
I know there have been several posts already about Sudoku-related questions, but I am
I know there are another questions about random in a range but none of
Note: I know this is wrong , but this is a technical requirement by
NOTE: I am aware of the SVN relocate switch, I just need to know
Note: marked as community wiki. In recent days, I've realized how little I know
Before I ask this, do note: I want this for debugging purposes. I know

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.