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

  • Home
  • SEARCH
  • 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 7404583
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T05:16:31+00:00 2026-05-29T05:16:31+00:00

I was trying Include extension method from http://damieng.com/blog/2010/05/21/include-for-linq-to-sql-and-maybe-other-providers , but it does not really

  • 0

I was trying Include extension method from http://damieng.com/blog/2010/05/21/include-for-linq-to-sql-and-maybe-other-providers, but it does not really work.

So I have this query in Linq-to-SQL:

 var items = dataContext.Items()
            .Where(x => x.Id < 100)             
            .ToList();

and data context has load option LoadWith(x => x.ItemImages).

That produces this SQL statement:

SELECT [t0].[Id], [t0].[Number], [t0].[Title], [t1].[Id] AS [Id2], [t1].[ItemId], [t1].[Url], (
SELECT COUNT(*)
FROM [dbo].[ItemImage] AS [t2]
WHERE [t2].[ItemId] = [t0].[Id]
) AS [value]
FROM [dbo].[Item] AS [t0]
LEFT OUTER JOIN [dbo].[ItemImage] AS [t1] ON [t1].[ItemId] = [t0].[Id]
WHERE [t0].[Id] < @p0
ORDER BY [t0].[Id], [t1].[Id]

Now, if I don’t use data load options, but rewrite query to:

 var items = dataContext.Items()
            .Where(x => x.Id < 100)
            .Select(x=>new Tuple<Item, EntitySet<ItemImage>>(x, x.ItemImages))
            .AsEnumerable()
            .Select(x=>x.Item1)
            .ToList();

The resulting SQL statement is the same:

SELECT [t0].[Id], [t0].[Number], [t0].[Title], [t1].[Id] AS [Id2], [t1].[ItemId], [t1].[Url], (
SELECT COUNT(*)
FROM [dbo].[ItemImage] AS [t2]
WHERE [t2].[ItemId] = [t0].[Id]
) AS [value]
FROM [dbo].[Item] AS [t0]
LEFT OUTER JOIN [dbo].[ItemImage] AS [t1] ON [t1].[ItemId] = [t0].[Id]
WHERE [t0].[Id] < @p0
ORDER BY [t0].[Id], [t1].[Id]

But, if I’m accessing items[0].ItemImages property, it does request to SQL Server to retrieve item images, so it looks like it had all required data to avoid additional queries, but materialization went wrong and it still does additional queries, although it could avoid them.

May I somehow fix this?

UPDATE: I was quite skeptical about performance, using LoadWith options, and thought that doing two queries (one for items, second for images) and mapping in code would be faster, than Linq2Sql generated single SQL query, but at least with my data amounts single query works faster, so it would be quite interesting to see a solution.

  • 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-29T05:16:32+00:00Added an answer on May 29, 2026 at 5:16 am

    The include method on my blog was only demonstrated and tested with a to-one relationship.

    By projecting a query that references an to-one it populates the LINQ to SQL identity cache. When any navigation property is later navigated it will hit that cache first.

    Unfortunately LINQ to SQL isn’t advanced enough to cache to-many associations in this way.

    Your options are either to use LoadWith or write a query that projects and groups by, e.g.

    var items = dataContext.ItemImages()
            .Where(x => x.Item.Id < 100)
            .Select(x => new Tuple<ItemImage, Item>(x, x.Item))
            .AsEnumerable()
            .Select(x = > x.Item1)
            .GroupBy(x => x.Item);
    

    The caveat to this query would be that you wouldn’t see any items that don’t have ItemImages.

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

Sidebar

Related Questions

Im trying to make an extension to Safari that authenticates with http://myphotodiary.com 's API.
I'm trying include this barcode source code Stefanhafeneger - Barcode github.com to my project
I'm currently trying to create a ToString - extension method for FaultException, that can
I'm trying to include an extension methods static class in a dynamically generated assembly,
I am trying to include my cascading style sheet into my TYPO3 extension. I
I am trying to extend the UIView class to include a setUserData method which
I'm tring to implement LINQ to Entity using Include extension. I have the following
I'm trying to create a Chrome extension that will reload touch.facebook.com every 10 minutes
I'm trying to include the Sparkle framework in my application. I don't really understand
I'm trying to include an image in an email, which is being sent using

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.