【问题标题】:Splitting parts of a List<T> into 2 List<T>'s and joining those 2将 List<T> 的部分拆分为 2 个 List<T> 并加入这 2 个
【发布时间】:2013-03-07 15:28:29
【问题描述】:

过去几个小时我一直被这个问题困扰,希望您能帮助我。

我有一个包含数据的列表(请参阅类:allItems),但我想拆分数据,因此类项包含所有数据,而公共列表包含子项。

在这个 sn-p 中发生的情况是,在 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;
}

【问题讨论】:

  • 请使用C#命名约定。属性名称应为 ProperCased(即 PunchInDate 而不是 punchInDate)。你的代码看起来像 java,这伤害了我的眼睛(和胃)
  • 将记住这一点以供将来参考,谢谢。
  • @highcore 我们需要找个时间喝一杯;我有一种感觉,我们要么相处得很好,要么打架。 :)
  • @JerKimball 这真的很难。我住在布宜诺斯艾利斯 =)

标签: c# list iteration


【解决方案1】:

听起来你可以用 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()
    };  

【讨论】:

  • @KasperMackenhauerJacobsen 呵呵 - 不,我只是输入得太快并且打错了 - 试试更新版本。 :)
  • 非常感谢!我真的需要学习 linq,但它不是课程的一部分,我认为这很愚蠢!无论如何,漂亮的答案,再次感谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-04-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-05-20
相关资源
最近更新 更多