我有一个对象列表。 每个对象都有一个整数和一个包含月份和年份值的DateTime变量。 我想遍历列表并填充列表,添加缺少的月份(数量为0),以便列表中显示所有连续月份。 实现这一目标的最佳方法是什么?
示例:原始列表
{Jan10,3},{Feb10,4},{Apr10,2},{May10,2},{Aug10,3},{Sep10,-3},{Oct10,6},{Nov10,3},{ Dec10,7},{Feb11,3}
新名单
{Jan10,3},{Feb10,4}, {Mar10,0} ,{Apr10,2},{May10,2},{Jun10,0},{Jul10,0} {Aug10,3},{Sep10, -3},{Oct10,6},{Nov10,3},{Dec10,7}, {Jan11,0} ,{Feb11,3}
I have a list of objects. Each object has an integer quantity and a DateTime variable which contains a month and year value. I'd like to traverse the list and pad the list by adding missing months (with quantity 0) so that all consecutive months are represented in the list. What would be the best way to accomplish this?
Example: Original List
{ Jan10, 3 }, { Feb10, 4 }, { Apr10, 2 }, { May10, 2 }, { Aug10, 3 }, { Sep10, -3 }, { Oct10, 6 }, { Nov10, 3 }, { Dec10, 7 }, { Feb11, 3 }
New List
{ Jan10, 3 }, { Feb10, 4 }, {Mar10, 0}, { Apr10, 2 }, { May10, 2 }, { Jun10, 0 }, { Jul10, 0 } { Aug10, 3 }, { Sep10, -3 }, { Oct10, 6 }, { Nov10, 3 }, { Dec10, 7 }, { Jan11, 0 }, { Feb11, 3 }
最满意答案
一种可能的算法是跟踪前一个月和当前月份。 如果先前和当前之间的差异是1个月,则将结果附加到当前。 如果差异超过一个月,请先添加缺失的月份,然后再复制当前月份。
Foo prev = months.First(); List<Foo> result = new List<Foo> { prev }; foreach (Foo foo in months.Skip(1)) { DateTime month = prev.Month; while (true) { month = month.AddMonths(1); if (month >= foo.Month) { break; } result.Add(new Foo { Month = month, Count = 0 }); } result.Add(foo); prev = foo; }结果:
01-01-2010 00:00:00: 3 01-02-2010 00:00:00: 4 01-03-2010 00:00:00: 0 01-04-2010 00:00:00: 2 01-05-2010 00:00:00: 2 01-06-2010 00:00:00: 0 01-07-2010 00:00:00: 0 01-08-2010 00:00:00: 3 01-09-2010 00:00:00: -3 01-10-2010 00:00:00: 6 01-11-2010 00:00:00: 3 01-12-2010 00:00:00: 7 01-01-2011 00:00:00: 0 01-02-2011 00:00:00: 3进行此编译所需的其他代码:
class Foo { public DateTime Month { get; set; } public int Count { get; set; } } List<Foo> months = new List<Foo> { new Foo{ Month = new DateTime(2010, 1, 1), Count = 3 }, new Foo{ Month = new DateTime(2010, 2, 1), Count = 4 }, new Foo{ Month = new DateTime(2010, 4, 1), Count = 2 }, new Foo{ Month = new DateTime(2010, 5, 1), Count = 2 }, new Foo{ Month = new DateTime(2010, 8, 1), Count = 3 }, new Foo{ Month = new DateTime(2010, 9, 1), Count = -3 }, new Foo{ Month = new DateTime(2010, 10, 1), Count = 6 }, new Foo{ Month = new DateTime(2010, 11, 1), Count = 3 }, new Foo{ Month = new DateTime(2010, 12, 1), Count = 7 }, new Foo{ Month = new DateTime(2011, 2, 1), Count = 3 } };注意:为简单起见,我没有处理原始列表为空的情况,但您应该在生产代码中执行此操作。
One possible algorithm is to keep track of the previous and current months. If the difference between previous and current is 1 month, append current to the result. If the difference is more than one month, add the missing months first, then afterwards copy the current month.
Foo prev = months.First(); List<Foo> result = new List<Foo> { prev }; foreach (Foo foo in months.Skip(1)) { DateTime month = prev.Month; while (true) { month = month.AddMonths(1); if (month >= foo.Month) { break; } result.Add(new Foo { Month = month, Count = 0 }); } result.Add(foo); prev = foo; }Results:
01-01-2010 00:00:00: 3 01-02-2010 00:00:00: 4 01-03-2010 00:00:00: 0 01-04-2010 00:00:00: 2 01-05-2010 00:00:00: 2 01-06-2010 00:00:00: 0 01-07-2010 00:00:00: 0 01-08-2010 00:00:00: 3 01-09-2010 00:00:00: -3 01-10-2010 00:00:00: 6 01-11-2010 00:00:00: 3 01-12-2010 00:00:00: 7 01-01-2011 00:00:00: 0 01-02-2011 00:00:00: 3Other code needed to make this compile:
class Foo { public DateTime Month { get; set; } public int Count { get; set; } } List<Foo> months = new List<Foo> { new Foo{ Month = new DateTime(2010, 1, 1), Count = 3 }, new Foo{ Month = new DateTime(2010, 2, 1), Count = 4 }, new Foo{ Month = new DateTime(2010, 4, 1), Count = 2 }, new Foo{ Month = new DateTime(2010, 5, 1), Count = 2 }, new Foo{ Month = new DateTime(2010, 8, 1), Count = 3 }, new Foo{ Month = new DateTime(2010, 9, 1), Count = -3 }, new Foo{ Month = new DateTime(2010, 10, 1), Count = 6 }, new Foo{ Month = new DateTime(2010, 11, 1), Count = 3 }, new Foo{ Month = new DateTime(2010, 12, 1), Count = 7 }, new Foo{ Month = new DateTime(2011, 2, 1), Count = 3 } };Note: For simplicity I haven't handled the case where the original list is empty but you should do this in production code.
遍历列表以使用.NET添加元素(Traverse list to add elements using .NET)我有一个对象列表。 每个对象都有一个整数和一个包含月份和年份值的DateTime变量。 我想遍历列表并填充列表,添加缺少的月份(数量为0),以便列表中显示所有连续月份。 实现这一目标的最佳方法是什么?
示例:原始列表
{Jan10,3},{Feb10,4},{Apr10,2},{May10,2},{Aug10,3},{Sep10,-3},{Oct10,6},{Nov10,3},{ Dec10,7},{Feb11,3}
新名单
{Jan10,3},{Feb10,4}, {Mar10,0} ,{Apr10,2},{May10,2},{Jun10,0},{Jul10,0} {Aug10,3},{Sep10, -3},{Oct10,6},{Nov10,3},{Dec10,7}, {Jan11,0} ,{Feb11,3}
I have a list of objects. Each object has an integer quantity and a DateTime variable which contains a month and year value. I'd like to traverse the list and pad the list by adding missing months (with quantity 0) so that all consecutive months are represented in the list. What would be the best way to accomplish this?
Example: Original List
{ Jan10, 3 }, { Feb10, 4 }, { Apr10, 2 }, { May10, 2 }, { Aug10, 3 }, { Sep10, -3 }, { Oct10, 6 }, { Nov10, 3 }, { Dec10, 7 }, { Feb11, 3 }
New List
{ Jan10, 3 }, { Feb10, 4 }, {Mar10, 0}, { Apr10, 2 }, { May10, 2 }, { Jun10, 0 }, { Jul10, 0 } { Aug10, 3 }, { Sep10, -3 }, { Oct10, 6 }, { Nov10, 3 }, { Dec10, 7 }, { Jan11, 0 }, { Feb11, 3 }
最满意答案
一种可能的算法是跟踪前一个月和当前月份。 如果先前和当前之间的差异是1个月,则将结果附加到当前。 如果差异超过一个月,请先添加缺失的月份,然后再复制当前月份。
Foo prev = months.First(); List<Foo> result = new List<Foo> { prev }; foreach (Foo foo in months.Skip(1)) { DateTime month = prev.Month; while (true) { month = month.AddMonths(1); if (month >= foo.Month) { break; } result.Add(new Foo { Month = month, Count = 0 }); } result.Add(foo); prev = foo; }结果:
01-01-2010 00:00:00: 3 01-02-2010 00:00:00: 4 01-03-2010 00:00:00: 0 01-04-2010 00:00:00: 2 01-05-2010 00:00:00: 2 01-06-2010 00:00:00: 0 01-07-2010 00:00:00: 0 01-08-2010 00:00:00: 3 01-09-2010 00:00:00: -3 01-10-2010 00:00:00: 6 01-11-2010 00:00:00: 3 01-12-2010 00:00:00: 7 01-01-2011 00:00:00: 0 01-02-2011 00:00:00: 3进行此编译所需的其他代码:
class Foo { public DateTime Month { get; set; } public int Count { get; set; } } List<Foo> months = new List<Foo> { new Foo{ Month = new DateTime(2010, 1, 1), Count = 3 }, new Foo{ Month = new DateTime(2010, 2, 1), Count = 4 }, new Foo{ Month = new DateTime(2010, 4, 1), Count = 2 }, new Foo{ Month = new DateTime(2010, 5, 1), Count = 2 }, new Foo{ Month = new DateTime(2010, 8, 1), Count = 3 }, new Foo{ Month = new DateTime(2010, 9, 1), Count = -3 }, new Foo{ Month = new DateTime(2010, 10, 1), Count = 6 }, new Foo{ Month = new DateTime(2010, 11, 1), Count = 3 }, new Foo{ Month = new DateTime(2010, 12, 1), Count = 7 }, new Foo{ Month = new DateTime(2011, 2, 1), Count = 3 } };注意:为简单起见,我没有处理原始列表为空的情况,但您应该在生产代码中执行此操作。
One possible algorithm is to keep track of the previous and current months. If the difference between previous and current is 1 month, append current to the result. If the difference is more than one month, add the missing months first, then afterwards copy the current month.
Foo prev = months.First(); List<Foo> result = new List<Foo> { prev }; foreach (Foo foo in months.Skip(1)) { DateTime month = prev.Month; while (true) { month = month.AddMonths(1); if (month >= foo.Month) { break; } result.Add(new Foo { Month = month, Count = 0 }); } result.Add(foo); prev = foo; }Results:
01-01-2010 00:00:00: 3 01-02-2010 00:00:00: 4 01-03-2010 00:00:00: 0 01-04-2010 00:00:00: 2 01-05-2010 00:00:00: 2 01-06-2010 00:00:00: 0 01-07-2010 00:00:00: 0 01-08-2010 00:00:00: 3 01-09-2010 00:00:00: -3 01-10-2010 00:00:00: 6 01-11-2010 00:00:00: 3 01-12-2010 00:00:00: 7 01-01-2011 00:00:00: 0 01-02-2011 00:00:00: 3Other code needed to make this compile:
class Foo { public DateTime Month { get; set; } public int Count { get; set; } } List<Foo> months = new List<Foo> { new Foo{ Month = new DateTime(2010, 1, 1), Count = 3 }, new Foo{ Month = new DateTime(2010, 2, 1), Count = 4 }, new Foo{ Month = new DateTime(2010, 4, 1), Count = 2 }, new Foo{ Month = new DateTime(2010, 5, 1), Count = 2 }, new Foo{ Month = new DateTime(2010, 8, 1), Count = 3 }, new Foo{ Month = new DateTime(2010, 9, 1), Count = -3 }, new Foo{ Month = new DateTime(2010, 10, 1), Count = 6 }, new Foo{ Month = new DateTime(2010, 11, 1), Count = 3 }, new Foo{ Month = new DateTime(2010, 12, 1), Count = 7 }, new Foo{ Month = new DateTime(2011, 2, 1), Count = 3 } };Note: For simplicity I haven't handled the case where the original list is empty but you should do this in production code.
发布评论