在过去的几个小时里,我一直被这个问题所困扰,希望你能帮助我。
我有一个充满数据的列表(请参阅类:allItems),但我想拆分数据,因此类项目将所有数据与包含子项目的项目内的公共列表一起保存。
此代码段中发生的情况是,在 for 循环期间,将具有相同值的项目添加到天数列表中,这是我在尝试将正确数量的条目添加到天数列表中时遇到的困难,因为同一周有多少次allStats 列表中的数字表示。
任何帮助将不胜感激,谢谢。
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;
}