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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T13:47:40+00:00 2026-05-15T13:47:40+00:00

I have a seemingly simple requirement, but i can’t figure out how to write

  • 0

I have a seemingly simple requirement, but i can’t figure out how to write it as a query that only has one round trip to the server.

Basically i have a simple table

CREATE TABLE Item
(
    id int not null identity(1,1),
    create datetime not null,
    close datetime --null means not closed yet
);

and what i want to do is over a range of time (say 1/1/2010 to 6/1/2010), for each month i need the number of items that were active in that month. an item is active if it was created either during or before that month and is either not closed (i.e. closed is null) or was closed after that month. So i translated that into a linq expression using a helper method:

//just returns the first day of every month inbetween min and max (inclusive)
private IEnumerable<DateTime> EnumerateMonths(DateTime Min, DateTime Max)
{
    var curr = new DateTime(Min.Year, Min.Month, 1);
    var Stop = new DateTime(Max.Year, Max.Month, 1).AddMonths(Max.Day == 1 ? 0 : 1);
    while(curr < Stop)
    {
        yield return curr;
        curr = curr.AddMonths(1);
    }
}

public List<DataPoint> GetBacklogByMonth(DateTime min, DateTime max)
{
    return EnumerateMonths(min, max)
        .Select(m => new DataPoint
                        {
                            Date = m,
                            Count = DB.Items.Where(s => s.Create <= m.AddMonths(1) && (!s.Close.HasValue || s.Close.Value >= m.AddMonths(1)))
                                    .Count()
                            }
            ).ToList();
}

which works perfectly, except each Count is a separate query so its super slow (a round trip for each month), so my question is how could i restructure this query to do this in one round trip to the server.

Initially i thought about doing some sort of group by so aggregate by month, but because each item could be ‘active’ in many different months i don’t think that would work.

Any suggestions?

  • 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-15T13:47:40+00:00Added an answer on May 15, 2026 at 1:47 pm

    I hate to answer my own question but here is what i did.

    What i really needed to do all along was a left join with a table of months then do a group and a count on the number of items for each month. a normal grouping on month wouldn’t work because then items would only get counted in a single month not all the ones they were active for. So I added a table Months containing just dates of the first of the month and did a left join on it. This operation needs to be done often enough that i figured it was worth adding a table for it.

    heres the final query:

            var joins = from m in DB.Months
                        from s in DB.Items
                        let nm = m.month.AddMonths(1)
                        where s.Create < nm && (!s.Close.HasValue || s.Close.Value >= nm) && m.month >= min && m.month <= max
                        select new { d = m.month, id = s.ID };
            var counts = from j in joins
                         group j by j.d into g
                         select new DataPoint { Date = g.Key, Count = g.Key > DateTime.Now ? 0 : g.Count() };
    

    I also added some code to make sure that months has the correct rows in it for my query.

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

Sidebar

Related Questions

I have a seemingly simple question, but can't find the answer. I have a
I am trying to complete a seemingly simple task that has turned into a
I have a seemingly simple problem though i am unable to get my head
I have a seemingly simple problem whereby I wish to reconcile two lists so
I have the following seemingly simple scenario, however I'm still pretty new to NHibernate.
I have a (seemingly) simple question to read in a string and print it
I can't make this seemingly simple call on my DomainService compile. I keep getting
It's a seemingly simple API. I thought that I was incorrectly releasing the AVAudioRecorder
I'm new to audio analysis, but need to perform a (seemingly) simple task. I
I have no idea. This causes seemingly random time-outs. These in turn break the

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.