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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T00:41:49+00:00 2026-06-05T00:41:49+00:00

Given a data with list of a months worth of data, I need to

  • 0

Given a data with list of a months worth of data, I need to calculate the sum for past 2 weeks. I could do this by breaking the list in 14 days and then 7 days and calculate the sum for each.

Is there an elegant way to achieve above?

example of data list looks like below:

startDate 2012-05-01
value 1

startDate 2012-05-02
value 1
........

startDate 2012-05-31
value 1

Thanks.

  • 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-05T00:41:51+00:00Added an answer on June 5, 2026 at 12:41 am

    Do you mean you want the sum for each of the past two weeks, or a single value across the past two weeks? The latter is easier than the former:

    DateTime end = DateTime.Today;
    // Note: inclusive at both ends, so this gives us 14 days in total
    DateTime start = end.AddDays(-13);
    var sum = data.Where(x => x.StartDate >= start && x.StartDate <= end)
                  .Sum(x => x.Value);
    

    Note that it’s important to evaluate DateTime.Today once here, as otherwise if the query executes “around” midnight you could end up with an inconsistent set of comparisons.

    If your data doesn’t include any future values, you can remove the end part of course. You should also carefully consider whether you really want to subtract 13 days or 14.

    EDIT: Okay, now you’ve clarified the requirement, it sounds like the simplest approach would actually be to execute two queries – one for the past 7 days, and one for the 7 days before that. Alternatively, you could use do it via grouping:

    DateTime end = DateTime.Today;
    // Note: inclusive at both ends, so this gives us 14 days in total
    DateTime start = end.AddDays(-13);
    var sum = data.OrderBy(x => x.StartDate)
                  .Where(x => x.StartDate >= start && x.StartDate <= end)
                  .GroupBy(x => ((int) (end - x.StartDate).TotalDays) / 7)
                  .Select(g => new { Start = g.First().StartDate,
                                     Sum = g.Sum(x => x.Value) });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given a list with a statically typed length (taking this as an example): data
What algorithm would you use to create an application that given appropriate data (list
Given data like this C1<-c(3,-999.000,4,4,5) C2<-c(3,7,3,4,5) C3<-c(5,4,3,6,-999.000) DF<-data.frame(ID=c(A,B,C,D,E),C1=C1,C2=C2,C3=C3) How do I go about removing
Given a data set like this; +-----+---------------------+--------+ | id | date | result |
Given data returned from: public IEnumerable<IEnumerable<string>> ReadFile(string filename) { List<List<string>> items; // Read entire
Given : data MyRecord a = MyRecord{list :: [a], other_fields :: Char, …} I
Given a list of tuples like this: dic = [(1,aa),(1,cc),(2,aa),(3,ff),(3,gg),(1,bb)] How to group items
Given the following code: abstract class Field { type T val data: List[T] def
Given data in the following format (tag_uri image_uri image_uri image_uri ...), I need to
Given data frame values are Group year Value A 2010 17 A 2011 18

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.