how to fill missing values from a list
Posted
by Stephane
on Stack Overflow
See other posts from Stack Overflow
or by Stephane
Published on 2010-05-19T13:27:28Z
Indexed on
2010/05/19
13:30 UTC
Read the original article
Hit count: 185
LINQ
I have an object containing a date and a count.
public class Stat
{
public DateTime Stamp {get; set;}
public int Count {get; set ;}
}
I have a Serie object that holds a list of thoses Stat plus some more info such as name and so on...
public class Serie
{
public string Name { get; set; }
public List<Stat> Data { get; set; }
...
}
Consider that I have a List of Serie but the series don't all contain the same Stamps. I need to fill in the missing stamps in all series with a default value.
I thought of an extension method with signature like this (please provide better name if you find one :) ) :
public static IEnumerable<Serie> Equalize(this IEnumerable<ChartSerie> series, int defaultCount)
this question seems to treat the same problem, but when querying directly the DB. of course I could loop through the dates and create another list. But is there any more elegant way to achieve this?
i.e.:
Serie A:
01.05.2010 1
03.05.2010 3
Serie B:
01.05.2010 5
02.05.2010 6
I should get :
Serie A :
01.05.2010 1
02.05.2010 0
03.05.2010 3
Serie B:
01.05.2010 5
02.05.2010 6
03.05.2010 0
© Stack Overflow or respective owner