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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T08:05:25+00:00 2026-05-18T08:05:25+00:00

I’m trying to figure out how to calculate difference from previous item, when the

  • 0

I’m trying to figure out how to calculate difference from previous item, when the data needs to be grouped.

I have data like this

City      Area  Date        Citizens
New York    1   2010.11.20  5
New York    1   2010.11.21  8
New York    1   2010.11.22  12
New York    1   2010.11.23  17
New York    1   2010.11.24  23
New York    1   2010.11.25  29

Chicago 1   2010.11.20  5
Chicago 1   2010.11.21  10
Chicago 1   2010.11.22  15
Chicago 1   2010.11.23  20
Chicago 1   2010.11.24  25
Chicago 1   2010.11.25  30

New York    2   2010.11.20  6
New York    2   2010.11.21  7
New York    2   2010.11.22  9
New York    2   2010.11.23  7
New York    2   2010.11.24  10
New York    2   2010.11.25  15

Chicago 2   2010.11.20  5
Chicago 2   2010.11.21  15
Chicago 2   2010.11.22  25
Chicago 2   2010.11.23  20
Chicago 2   2010.11.24  25
Chicago 2   2010.11.25  30

and I need to add a column “Increase” for each are for each city, which would be calculated by subtracting previous Citizens count from current.

The expected result is like this

City    Area    Date    Citizens    Increase
New York    1   2010.11.20  5   5
New York    1   2010.11.21  8   3
New York    1   2010.11.22  12  4
New York    1   2010.11.23  17  5
New York    1   2010.11.24  23  6
New York    1   2010.11.25  29  7

Chicago 1   2010.11.20  5   5
Chicago 1   2010.11.21  10  5
Chicago 1   2010.11.22  15  5
Chicago 1   2010.11.23  20  5
Chicago 1   2010.11.24  25  5
Chicago 1   2010.11.25  30  5

New York    2   2010.11.20  6   6
New York    2   2010.11.21  7   1
New York    2   2010.11.22  9   2
New York    2   2010.11.23  7   -2
New York    2   2010.11.24  10  3
New York    2   2010.11.25  15  5

Chicago 2   2010.11.20  5   5
Chicago 2   2010.11.21  15  10
Chicago 2   2010.11.22  25  10
Chicago 2   2010.11.23  20  -5
Chicago 2   2010.11.24  25  5
Chicago 2   2010.11.25  30  5

I wonder if this can be done with a single linq query, avoiding
“foreach (c in cities)
foreach (a in area)
….”

The problem is how to calculate line “7” where simply diff to previous record would be -24, when is should be 5.

Here’s a sample code :

using System;
using System.Collections.Generic;

namespace ConsoleApplication1
{
    public class MyObject
    {
        public int ID { get; set; }
        public string City { get;set; }
        public DateTime Date { get; set; }
        public int Value { get; set; }
        public int DiffToPrev { get; set; }
    }

    class Program
    {

        static void Main()
        {
            var list = new List<MyObject>
              {
                new MyObject {ID= 1, City = "New York",Date = DateTime.Now,          Value = 5},
                new MyObject {ID= 1, City = "New York",Date = DateTime.Now.AddDays(1),Value = 8},
                new MyObject {ID= 1, City = "New York",Date = DateTime.Now.AddDays(2),Value = 12},
                new MyObject {ID= 1, City = "New York",Date = DateTime.Now.AddDays(3),Value = 17},
                new MyObject {ID= 1, City = "New York",Date = DateTime.Now.AddDays(4),Value = 23},
                new MyObject {ID= 1, City = "New York",Date = DateTime.Now.AddDays(5),Value = 29},

                new MyObject {ID= 1, City = "Chicago",Date = DateTime.Now,           Value = 5},
                new MyObject {ID= 1, City = "Chicago",Date = DateTime.Now.AddDays(1),Value = 10},
                new MyObject {ID= 1, City = "Chicago",Date = DateTime.Now.AddDays(2),Value = 15},
                new MyObject {ID= 1, City = "Chicago",Date = DateTime.Now.AddDays(3),Value = 20},
                new MyObject {ID= 1, City = "Chicago",Date = DateTime.Now.AddDays(4),Value = 25},
                new MyObject {ID= 1, City = "Chicago",Date = DateTime.Now.AddDays(5),Value = 30},

                new MyObject {ID= 2, City = "New York",Date = DateTime.Now,          Value = 6},
                new MyObject {ID= 2, City = "New York",Date = DateTime.Now.AddDays(1),Value = 7},
                new MyObject {ID= 2, City = "New York",Date = DateTime.Now.AddDays(2),Value = 9},
                new MyObject {ID= 2, City = "New York",Date = DateTime.Now.AddDays(3),Value = 7},
                new MyObject {ID= 2, City = "New York",Date = DateTime.Now.AddDays(4),Value = 10},
                new MyObject {ID= 2, City = "New York",Date = DateTime.Now.AddDays(5),Value = 15},

                new MyObject {ID= 2, City = "Chicago",Date = DateTime.Now,           Value = 5},
                new MyObject {ID= 2, City = "Chicago",Date = DateTime.Now.AddDays(1),Value = 15},
                new MyObject {ID= 2, City = "Chicago",Date = DateTime.Now.AddDays(2),Value = 25},
                new MyObject {ID= 2, City = "Chicago",Date = DateTime.Now.AddDays(3),Value = 20},
                new MyObject {ID= 2, City = "Chicago",Date = DateTime.Now.AddDays(4),Value = 25},
                new MyObject {ID= 2, City = "Chicago",Date = DateTime.Now.AddDays(5),Value = 30},               
            };

        }
    }
}
  • 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-18T08:05:25+00:00Added an answer on May 18, 2026 at 8:05 am

    You can do in this way:

    var newList = list.GroupBy(x => new { x.City, x.ID })
    .Select
    (
        x =>
        {
            var subList = x.OrderBy(y => y.Date).ToList();
            return subList.Select((y, idx) => new MyObject
            {
                ID = y.ID,
                City = y.City,
                Date = y.Date,
                Value = y.Value,
                DiffToPrev = (idx == 0) ? y.Value : y.Value - subList.ElementAt(idx-1).Value 
            });
        }
    )
    .SelectMany(x => x)
    .ToList();
    

    Anyway, I think in this case a foreach statement is clearer (and not longer), e.g.:

    List<MyObject> newList = new List<MyObject>();
    foreach (var grp in list.GroupBy(x => new { x.City, x.ID }))
    {
        MyObject prev = null;
        foreach (var obj in grp.OrderBy(y => y.Date))
        {
            newList.Add(new MyObject
            {
                ID = obj.ID,
                City = obj.City,
                Date = obj.Date,
                Value = obj.Value,
                DiffToPrev = (prev == null) ? obj.Value : obj.Value - prev.Value
            });
            prev = obj;
        }
    }
    

    P.S.
    obviously (depending on your needs) in the foreach code you can set the DiffToPrev directly on the existing object (obj) instead of creating a new one, making optional the creation of newList.


    Results:

    ID: 1,City: New York,Date: 21/11/2010 12:52:40,Value: 5,Diff: 5
    ID: 1,City: New York,Date: 22/11/2010 12:52:40,Value: 8,Diff: 3
    ID: 1,City: New York,Date: 23/11/2010 12:52:40,Value: 12,Diff: 4
    ID: 1,City: New York,Date: 24/11/2010 12:52:40,Value: 17,Diff: 5
    ID: 1,City: New York,Date: 25/11/2010 12:52:40,Value: 23,Diff: 6
    ID: 1,City: New York,Date: 26/11/2010 12:52:40,Value: 29,Diff: 6
    ID: 1,City: Chicago,Date: 21/11/2010 12:52:40,Value: 5,Diff: 5
    ID: 1,City: Chicago,Date: 22/11/2010 12:52:40,Value: 10,Diff: 5
    ID: 1,City: Chicago,Date: 23/11/2010 12:52:40,Value: 15,Diff: 5
    ID: 1,City: Chicago,Date: 24/11/2010 12:52:40,Value: 20,Diff: 5
    ID: 1,City: Chicago,Date: 25/11/2010 12:52:40,Value: 25,Diff: 5
    ID: 1,City: Chicago,Date: 26/11/2010 12:52:40,Value: 30,Diff: 5
    ID: 2,City: New York,Date: 21/11/2010 12:52:40,Value: 6,Diff: 6
    ID: 2,City: New York,Date: 22/11/2010 12:52:40,Value: 7,Diff: 1
    ID: 2,City: New York,Date: 23/11/2010 12:52:40,Value: 9,Diff: 2
    ID: 2,City: New York,Date: 24/11/2010 12:52:40,Value: 7,Diff: -2
    ID: 2,City: New York,Date: 25/11/2010 12:52:40,Value: 10,Diff: 3
    ID: 2,City: New York,Date: 26/11/2010 12:52:40,Value: 15,Diff: 5
    ID: 2,City: Chicago,Date: 21/11/2010 12:52:40,Value: 5,Diff: 5
    ID: 2,City: Chicago,Date: 22/11/2010 12:52:40,Value: 15,Diff: 10
    ID: 2,City: Chicago,Date: 23/11/2010 12:52:40,Value: 25,Diff: 10
    ID: 2,City: Chicago,Date: 24/11/2010 12:52:40,Value: 20,Diff: -5
    ID: 2,City: Chicago,Date: 25/11/2010 12:52:40,Value: 25,Diff: 5
    ID: 2,City: Chicago,Date: 26/11/2010 12:52:40,Value: 30,Diff: 5
    ID: 1,City: New York,Date: 21/11/2010 12:52:40,Value: 5,Diff: 5
    ID: 1,City: New York,Date: 22/11/2010 12:52:40,Value: 8,Diff: 3
    ID: 1,City: New York,Date: 23/11/2010 12:52:40,Value: 12,Diff: 4
    ID: 1,City: New York,Date: 24/11/2010 12:52:40,Value: 17,Diff: 5
    ID: 1,City: New York,Date: 25/11/2010 12:52:40,Value: 23,Diff: 6
    ID: 1,City: New York,Date: 26/11/2010 12:52:40,Value: 29,Diff: 6
    ID: 1,City: Chicago,Date: 21/11/2010 12:52:40,Value: 5,Diff: 5
    ID: 1,City: Chicago,Date: 22/11/2010 12:52:40,Value: 10,Diff: 5
    ID: 1,City: Chicago,Date: 23/11/2010 12:52:40,Value: 15,Diff: 5
    ID: 1,City: Chicago,Date: 24/11/2010 12:52:40,Value: 20,Diff: 5
    ID: 1,City: Chicago,Date: 25/11/2010 12:52:40,Value: 25,Diff: 5
    ID: 1,City: Chicago,Date: 26/11/2010 12:52:40,Value: 30,Diff: 5
    ID: 2,City: New York,Date: 21/11/2010 12:52:40,Value: 6,Diff: 6
    ID: 2,City: New York,Date: 22/11/2010 12:52:40,Value: 7,Diff: 1
    ID: 2,City: New York,Date: 23/11/2010 12:52:40,Value: 9,Diff: 2
    ID: 2,City: New York,Date: 24/11/2010 12:52:40,Value: 7,Diff: -2
    ID: 2,City: New York,Date: 25/11/2010 12:52:40,Value: 10,Diff: 3
    ID: 2,City: New York,Date: 26/11/2010 12:52:40,Value: 15,Diff: 5
    ID: 2,City: Chicago,Date: 21/11/2010 12:52:40,Value: 5,Diff: 5
    ID: 2,City: Chicago,Date: 22/11/2010 12:52:40,Value: 15,Diff: 10
    ID: 2,City: Chicago,Date: 23/11/2010 12:52:40,Value: 25,Diff: 10
    ID: 2,City: Chicago,Date: 24/11/2010 12:52:40,Value: 20,Diff: -5
    ID: 2,City: Chicago,Date: 25/11/2010 12:52:40,Value: 25,Diff: 5
    ID: 2,City: Chicago,Date: 26/11/2010 12:52:40,Value: 30,Diff: 5
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have some data like this: 1 2 3 4 5 9 2 6
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have a text area in my form which accepts all possible characters from
Does anyone know how can I replace this 2 symbol below from the string
I am trying to loop through a bunch of documents I have to put

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.