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},
};
}
}
}
You can do in this way:
Anyway, I think in this case a foreach statement is clearer (and not longer), e.g.:
P.S.
obviously (depending on your needs) in the foreach code you can set the
DiffToPrevdirectly on the existing object (obj) instead of creating a new one, making optional the creation ofnewList.Results: