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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T20:56:13+00:00 2026-06-12T20:56:13+00:00

I have this linq query i’m using and it’s taking 50 seconds to run

  • 0

I have this linq query i’m using and it’s taking 50 seconds to run when i am running it my asp.net application, however the same query executes in 500ms in LinqPad and Sql Management Studio.

I even took the query from the SQL Profiler and ran it again in SQL Management Studio and it takes around 500ms. What overhead Linq could be doing, that an extra 49s??

Below is the code for reference, thanks for your help.

var rCampaign =
    (from a in db.AdCreative
     join h in db.AdHit on a.ID equals h.AdID into gh
     join l in db.AdGroup_Location on a.AdGroupID equals l.AdGroupID into gj
     from subloc in gj.DefaultIfEmpty()
     from subhits in gh.DefaultIfEmpty()
     where a.AdGroup.AdHost.Select(q => q.ID).Contains(rPlatform.ID) &&
           a.AdGroup.AdPublisher.Select(q => q.ID).Contains(rPublisher.ID) &&
           a.AdDimensionID == AdSize &&
           a.AdGroup.Campaign.Starts <= rNow &&
           a.AdGroup.Campaign.Ends >= rNow &&
           subhits.HitType == 1 &&
           (subloc == null || subloc.LocationID == rLocationID)
     select new {
         ID = a.ID,
         Name = a.Name,
         Spent = (subhits.AdDimension != null) ? ((double)subhits.AdDimension.Credit / 1000) : 0,
         CampaignID = a.AdGroup.Campaign.ID,
         CampaignName = a.AdGroup.Campaign.Name,
         CampaignBudget = a.AdGroup.Campaign.DailyBudget
     }).GroupBy(adgroup => adgroup.ID)
       .Select(adgroup => new {
           ID = adgroup.Key,
           Name = adgroup.FirstOrDefault().Name,
           Spent = adgroup.Sum(q => q.Spent),
           CampaignID = adgroup.FirstOrDefault().CampaignID,
           CampaignName = adgroup.FirstOrDefault().CampaignName,
           CampaignBudget = adgroup.FirstOrDefault().CampaignBudget,
       })
      .GroupBy(q => q.CampaignID)
      .Select(campaigngroup => new {
          CampaignID = campaigngroup.Key,
          DailyBudget = campaigngroup.FirstOrDefault().CampaignBudget,
          Consumed = campaigngroup.Sum(q => q.Spent),
          RemainningCredit = campaigngroup.FirstOrDefault().CampaignBudget - campaigngroup.Sum(q => q.Spent),
          Ads = campaigngroup.Select(ag => new {
              ID = ag.ID,
              Name = ag.Name,
              Spent = ag.Spent
          }).OrderBy(q => q.Spent)
      })
      .Where(q => q.Consumed <= q.DailyBudget).OrderByDescending(q => q.RemainningCredit).First();
  • 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-06-12T20:56:15+00:00Added an answer on June 12, 2026 at 8:56 pm

    There are a few ways you can simplify that query:

    • select into lets you keep it all in query syntax.
    • The join ... into/from/DefaultIfMany constructs implementing left joins can be replaced with join ... into construcs representing group joins.
    • Some of the groups near the end cannot be empty, so FirstOrDefault is unnecessary.
    • Some of the where conditions can be moved up to the top before the query gets complicated.

    Here’s the stab I took at it. The revisions were significant, so it might need a little debugging:

    var rCampaign = ( 
        from a in db.AdCreative
    
        where a.AdDimensionID == AdSize &&
              a.AdGroup.Campaign.Starts <= rNow &&
              a.AdGroup.Campaign.Ends >= rNow &&
              a.AdGroup.AdHost.Select(q => q.ID).Contains(rPlatform.ID) &&
              a.AdGroup.AdPublisher.Select(q => q.ID).Contains(rPublisher.ID)
    
        join hit in db.AdHit.Where(h => h.HitType == 1 && h.LocationID == rLocationID)
            on a.ID equals hit.AdID
            into hits
    
        join loc in db.AdGroup_Location
            on a.AdGroupID equals loc.AdGroupID
            into locs
    
        where !locs.Any() || locs.Any(l => l.LocationID == rLocationID)
    
        select new {
            a.ID,
            a.Name,
            Spent = hits.Sum(h => h.AdDimension.Credit / 1000) ?? 0,
            CampaignID = a.AdGroup.Campaign.ID,
            CampaignName = a.AdGroup.Campaign.Name,
            CampaignBudget = a.AdGroup.Campaign.DailyBudget,
        } into adgroup
        group adgroup by adgroup.CampaignID into campaigngroup
        select new
        {
            CampaignID = campaigngroup.Key,
            DailyBudget = campaigngroup.First().CampaignBudget,
            Consumed = campaigngroup.Sum(q => q.Spent),
            RemainingCredit = campaigngroup.First().CampaignBudget - campaigngroup.Sum(q => q.Spent),
            Ads = campaigngroup.Select(ag => new {
                ag.ID,
                ag.Name,
                ag.Spent,
            }).OrderBy(q => q.Spent)
        } into q
        where q.Consumed <= q.DailyBudget
        orderby q.RemainingCredit desc)
        .First()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need some help with a LINQ query in VB.Net, please. I have this
Suppose I have this LINQ query (on .NET 4.0) : IEnumerable<Service> listService = (from
I have this contact list which I'm building using LINQ to SQL. The query
I have a list using this Linq query filterEntities = (from list in filterEntities
I have this linq query that works well (although it may be written better,
So I have this linq query clause. var ctx = new Context(); IQueryable<Users> consulta
I have this following IEnumerable LINQ query: var query = from p in Enumerable.Range(2,
I'm having an issue getting a LINQ query to work. I have this XML:
I'm having a misunderstanding problem with this use of Linq Query I do have
I have a Linq query that looks something like this: var myPosse = from

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.