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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T07:02:42+00:00 2026-05-23T07:02:42+00:00

I have one Linq to SQL query that simply joins three tables and then

  • 0

I have one Linq to SQL query that simply joins three tables and then performs grouping. Here is the query:

(from s in mktActualSales
                      join p in sysPeriods on s.PeriodID equals p.PeriodID
                      join d in setupDesignations on s.PositionID equals d.DesignationID
                      group new { s, p } by new{d.Title,d.DesignationID} into temping
                      select
                          new 
                          {
                                 SPOPosition = temping.Key.Title,
                                 SalesPeriods = temping.Select(x=>new {PeriodID = x.p.PeriodID, StartDate = x.p.StartDate, EndDate = x.p.EndDate, SaleTargetID = x.s.ActualSaleID, IsApproved = x.s.IsApproved}),
                                 PositionID = temping.Key.DesignationID
                          }).Take(5)

When I inspect SQL generated (executed) by this query in LinqPad, there are 6 SQL statements; first one performs join and grouping and rest of queries are the same just calling same query over and over again with different parameters. Obviously parameters are the values included in Key of the group.

It makes me believe that for a record with 130 groups Linq will hit the database 131 times. How can I save so much hits to the database? Should I perform grouping after loading data into memory i.e after calling ToList on joined query?

  • 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-23T07:02:42+00:00Added an answer on May 23, 2026 at 7:02 am

    (I am assuming mktActualSales is your table and you missed putting the Datacontext in the question.)

    If it is your table you should have a look at LoadOptions on your DataContext. With this you can eager load some of the related data and this might prevent the repetitive queries.

    http://msdn.microsoft.com/en-us/library/system.data.linq.dataloadoptions.aspx

    If it is not a table, have a look on how you populate mktActualSales but judging from your question I assume it is a table.

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

Sidebar

Related Questions

I have this one gigantic complex LINQ to SQL query that I need to
I have a complex LINQ to SQL to query, which joins onto two tables
I have a Linq to Entities query like this one: var results = from
I have a very complex Linq to SQL query that returns a result set
I have been using LINQ to SQL for a while, and there is one
Some background: I have an database that I want to use linq-to-sql to update
I have a query in linqtosql that returns a LabelNumber: var q = from
I'm learning Linq to Sql; Is it always the case that you simply pass
I have a somewhat simple query in SQL that I would like to convert
We're currently a Linq to SQL shop but evaluating Entity Framework. One thing that

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.