I have been stumped on this problem for the last couple of hours, I hope you can help me.
I have a list filled with data (see class: allItems), but I want to split up the data, so the class items hold all of it with the public List inside items holding subItems.
What happens in this snippet is that during the for loop the item is added with the same values to the days list, which is where I am stumped on trying to add the correct number of entries to the days list given how many times the same week number figurates in the allStats list.
Any help would be appreciated, thanks.
public class allItems
{
public DateTime PunchInDate { get; set; }
public DateTime PunchOutDate { get; set; }
public DayOfWeek DayOfWeek { get; set; }
public int WeekNumber { get; set; }
public int MonthNumber { get; set; }
public bool PunchedInLate { get; set; }
public bool PunchedOutLate { get; set; }
}
public class items
{
public int WeekNumber { get; set; }
public int MonthNumber { get; set; }
public List<subItems> Days { get; set; }
}
public class subItems
{
public bool PunchedInLate { get; set; }
public bool PunchedOutLate { get; set; }
public DateTime PunchInDate { get; set; }
public DateTime PunchOutDate { get; set; }
public DayOfWeek DayOfWeek { get; set; }
}
protected int getNumberOfWeeks(List<allItems> list, int numberToFind)
{
List<allItems> results = list.FindAll(
delegate(allItems ai)
{
return ai.WeekNumber == numberToFind;
}
);
return results.Count;
}
public List<items> getStats(string userId, string type)
{
List<allItems> allStats = getAllStats(userId, "week");
List<items> stats = new List<items>();
foreach (allItems allItem in allStats)
{
items item = new items();
subItems subItem = new subItems();
List<subItems> Days = new List<subItems>();
item.MonthNumber = allItem.MonthNumber;
item.WeekNumber = allItem.WeekNumber;
item.Days = Days;
int numberOfWeeks = getNumberOfWeeks(allStats, allItem.WeekNumber);
for (int i = 0; i < numberOfWeeks; i++)
{
subItem.DayOfWeek = allItem.DayOfWeek;
subItem.PunchedInLate = allItem.PunchedInLate;
subItem.PunchedOutLate = allItem.PunchedOutLate;
subItem.punchInDate = allItem.PunchInDate;
subItem.PunchOutDate = allItem.PunchOutDate;
Days.Add(subItem);
}
items result = stats.Find(week => week.WeekNumber == allItem.WeekNumber);
if (result == null)
{
stats.Add(item);
}
}
return stats;
}