2013-03-07 46 views
0

在过去的几个小时中,我一直在困扰这个问题,我希望你能帮助我。将列表<T>的部分拆分为2列表<T>和加入这些2

我有一个填充数据的列表(请参阅class: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; 
} 
+3

请使用'C#'命名约定。属性名称应该是'ProperCased'(I.E'PunchInDate'而不是'punchInDate')。你的代码看起来像java,这会伤害我的眼睛(和胃) – 2013-03-07 15:31:23

+0

请记住这一点以备将来参考,谢谢。 – mackwerk 2013-03-07 15:38:45

+0

@highcore我们需要在某个时间喝点饮料;我有一种感觉,我们要么相处得很好,要么进入战斗。 :) – JerKimball 2013-03-07 15:39:48

回答

3

听起来像是你可以做到这一点使用LINQ:

var allItemsList = new List<allItems>() { ... fill in items ... }; 
var asItems = 
    from item in allItemsList 
    group item by new { week = item.weekNumber, month = item.monthNumber } into byWeekMonth 
    select new items() 
    { 
     weekNumber = byWeekMonth.Key.week, 
     monthNumber = byWeekMonth.Key.month, 
     days = byWeekMonth.Select(si => 
      new subItems() 
      { 
       punchedInLate = si.punchedInLate, 
       punchedOutLate = si.punchedOutLate, 
       // etc, other fields 
      }).ToList() 
    }; 
+0

'key'在intellisense中给出了这个错误''Statistics.allItems'没有包含'Key'的定义,没有'Key'的扩展方法接受'Statistics.allItems'类型的第一个参数'可以找到(你是否错过了使用指令或程序集引用?)' 虽然,如果那小块代码可以完成这个技巧,可能会很可爱。 – mackwerk 2013-03-07 15:50:01

+0

@KasperMackenhauerJacobsen嘿 - 不,我只是输入太快而且错误 - 尝试更新的版本。 :) – JerKimball 2013-03-07 15:56:46

+0

非常感谢!我真的需要学习linq,但这不是课程的一部分,我认为这是愚蠢的! 无论如何,美丽的答案,再次感谢! – mackwerk 2013-03-07 16:11:09