Fill Values in List

S

Shapper

Hello,

I have a List<Tuple<DateTime, Int32> LIST1 width:

22-03-2012 00:00:00 12
22-03-2012 01:00:00 22
22-03-2012 04:00:00 45
24-03-2012 05:00:00 20

I would like to:
1 - Create a new list, LIST2, with all dates and hours between 22-03-2012 and 25-03-2012 with 0 as value.

2 - Fill LIST2 with LIST1 items. I think I should use UNION.

How can I do this?

Thank You,
Miguel
 
A

Arne Vajhøj

I have a List<Tuple<DateTime, Int32> LIST1 width:

22-03-2012 00:00:00 12
22-03-2012 01:00:00 22
22-03-2012 04:00:00 45
24-03-2012 05:00:00 20

I would like to:
1 - Create a new list, LIST2, with all dates and hours between 22-03-2012 and 25-03-2012 with 0 as value.

2 - Fill LIST2 with LIST1 items. I think I should use UNION.

How can I do this?

Something like:

List<Tuple<DateTime, int>> list2 = list1.Where(tp => tp.Item2 ==
0).Where(tp => tp.Item1 >= new DateTime(2012, 3, 22) && tp.Item1 < new
DateTime(2012, 3, 26)).ToList();

I am not quite sure I understand the second question.

Arne
 
S

Shapper

Something like:

List<Tuple<DateTime, int>> list2 = list1.Where(tp => tp.Item2 ==
0).Where(tp => tp.Item1 >= new DateTime(2012, 3, 22) && tp.Item1 < new
DateTime(2012, 3, 26)).ToList();

I am not quite sure I understand the second question.

Arne

Hello Arne,

Basically LIST1 is similar to the one that you created but some dates / hours are missing ... So my idea is to create a LIST2 which contains all datesand hours but the Int is 0.

Then I pick the Dates/Hours that exist in LIST1 and place them in LIST2. SoI get a list, LIST2, that contains all dates/hours in the given date rangewithout gaps.

I was thinking in doing this with UNION or something similar.

Example:

LIST 1
22-03-2012 23:00:00 12
23-03-2012 00:00:00 22
23-03-2012 02:00:00 45

LIST 2
22-03-2012 23:00:00 0
23-03-2012 00:00:00 0
23-03-2012 01:00:00 0
23-03-2012 02:00:00 0

My idea was to use a Set operation that would get me the following:

LIST 2
22-03-2012 23:00:00 12
23-03-2012 00:00:00 22
23-03-2012 01:00:00 0
23-03-2012 02:00:00 45

Is it more clear what I am trying to do?

Thank You,
Miguel
 
A

Arne Vajhøj

Basically LIST1 is similar to the one that you created but some dates / hours are missing ... So my idea is to create a LIST2 which contains all dates and hours but the Int is 0.

Then I pick the Dates/Hours that exist in LIST1 and place them in LIST2. So I get a list, LIST2, that contains all dates/hours in the given date range without gaps.

I was thinking in doing this with UNION or something similar.

Example:

LIST 1
22-03-2012 23:00:00 12
23-03-2012 00:00:00 22
23-03-2012 02:00:00 45

LIST 2
22-03-2012 23:00:00 0
23-03-2012 00:00:00 0
23-03-2012 01:00:00 0
23-03-2012 02:00:00 0

My idea was to use a Set operation that would get me the following:

LIST 2
22-03-2012 23:00:00 12
23-03-2012 00:00:00 22
23-03-2012 01:00:00 0
23-03-2012 02:00:00 45

Is it more clear what I am trying to do?

Ah - you want to fill the list for all the missing hours with
zero values?

Arne
 
A

Arne Vajhøj

Ah - you want to fill the list for all the missing hours with
zero values?

Like:

List<Tuple<DateTime, int>> list2 = new List<Tuple<DateTime,
int>>();
int ix1 = 0;
for(DateTime dt = new DateTime(2012, 3, 22, 0, 0, 0); dt <
new DateTime(2012, 3, 25, 0, 0, 0); dt = dt.AddHours(1))
{
if(ix1 < list1.Count && dt == list1[ix1].Item1)
{
list2.Add(list1[ix1]);
ix1++;
}
else
{
list2.Add(new Tuple<DateTime,int>(dt, 0));
}
}


Arne
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top