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
  • 3 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

I have following legacy database setup: CREATE TABLE `categories` ( `id` int(11) NOT NULL
I have two database tables with the following structure: actions: action_id int(11) primary key
I'm using EF v1. I have following tables: CREATE TABLE category ( category_id int
I have the following tables in my database: Employee Table which consists of Name,
I have a database with 2 tables: Table 1: CREATE TABLE IF NOT EXISTS
I have the following database table created thus: CREATE TABLE AUCTIONS ( ARTICLE_NO VARCHAR(20),
I have the following table in a SQLite3 database: CREATE TABLE overlap_results ( neighbors_of_annotation
I have a database that is similar to the following: create table Store(storeId) create
I have the following two tables in my database (the indexing is not complete
In an SQL Server 2008 database, I have tables such as: CREATE TABLE t_DeviceType

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.