分割列表的各个部分<分为2个列表's,并将它们连接起来

本文关键字:列表 起来 连接 个部 分为 分割 2个 | 更新日期: 2023-09-27 18:03:01

过去几个小时我一直被这个问题难住了,希望你能帮助我。

我有一个充满数据的列表(参见class: allItems),但是我想拆分数据,所以类项包含所有数据,而公共列表内部的项包含子条目。

在此代码片段中发生的事情是,在for循环期间,条目以相同的值添加到days列表中,这就是我在尝试将正确的条目数添加到days列表时遇到的困难,因为给定相同的周数在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;
}

分割列表的各个部分<分为2个列表<T>'s,并将它们连接起来

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