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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T01:07:38+00:00 2026-05-18T01:07:38+00:00

I have a database with the following tables: create table Categories ( Id int

  • 0

I have a database with the following tables:

create table Categories (
  Id int primary key,
  Name nvarchar(256) not null)

create table Products (
  Id int primary key,
  Name nvarchar(256) not null,
  CategoryId int not null,
  foreign key (CategoryId) references Categories (Id))

Using DataLoadOptions, I can write this:

DataLoadOptions dlo = new DataLoadOptions();
dlo.LoadWith<Category>(c => c.Products);

and get all of the category and product information in one query.

However, db.Categories.First(), db.Categories.FirstOrDefault(), and db.Categories.Take(10) will execute a query that only grabs a subset of entries from the Categories table, and nothing else. Essentially, this:

SELECT TOP (1) Id, Name FROM Categories
-- and
SELECT TOP (10) Id, Name FROM Categories

Accessing the Products property of a Category instance will result in another query execution.

I’ve discovered a few ways to get around this.

// For First():
var category = db.Categories.Single(c => c == db.Categories.First());

// For FirstOrDefault():
var category = db.Categories.SingleOrDefault(c => c == db.Categories.First());

// For Take(10):
var categories = db.Categories.Where(c => db.Categories.Take(10).Contains(c));

All of the LINQ statements above will return all of the category and product information for a subset of Categories entries in one query each.

Does anyone know if there is a better or more efficient way of achieving this? Thanks.

  • 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-18T01:07:39+00:00Added an answer on May 18, 2026 at 1:07 am

    You should read about Skip, Take and loading related collections here:

    LINQ to SQL Take w/o Skip Causes Multiple SQL Statements

    The bad news is that the solution won’t work for you. CompiledQuery is allergic to LoadOptions – you’ll get a runtime error.


    Accessing the Products property of a
    Category instance will result in
    another query execution.

    Nitty-pick. This isn’t quite true. By the time your call to First returns, the Products will be fetched. The contract of LoadsWith is that your data gets fetched. The problem is that it doesn’t guarantee the most efficient queries are used to do the fetching (a single query with ROWNUMBER in this case).

    Here’s a method implementation that confirms:

            DataClasses1DataContext myDC = new DataClasses1DataContext();
            var myOptions = new System.Data.Linq.DataLoadOptions();
            myOptions.LoadWith<Customer>(c => c.Orders);
            myDC.LoadOptions = myOptions;
    
            myDC.Log = Console.Out;
    
    
            myDC.Customers.Take(10).ToList();
    

    1 query is issued for the Customers, followed by 10 queries, 1 for each customer’s orders. These 10 queries occur even though no Customer instances’ Orders properties were accessed.


    This article by KWatkins demonstrates how to use LoadOptions with CompiledQuery. This opens up CompiledQuery as a way to lock in the Skip(x) even when x may be 0.

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

Sidebar

Related Questions

If i have the following database table names in a HTBTM relantionship: **dressess** id
I have a database table that holds parent and child records much like a
I've a very simple database with following schema: video (id, title, description) category (id,
I have the following character encoding issue, somehow I have managed to save data
Situation: I got news. NEws are posted in multiple categories. Issue: How to store
I am creating a database that is trying to access values from a foreign
Having briefly studied databases in college, I haven't worked with them since and have
I am designing a system to record and report on daily measurement data. The
Is it possible using just SQL and MySQL to get the OUTPUT below? SAMPLE

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.