拆分List< T>的部分。进入2 List< T>并加入那2个 [英] Splitting parts of a List<T> into 2 List<T>'s and joining those 2

查看:106
本文介绍了拆分List< T>的部分。进入2 List< T>并加入那2个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

过去几个小时我一直对这个问题感到难过,我希望你能帮助我。

I have been stumped on this problem for the last couple of hours, I hope you can help me.

我有一个填充数据的清单(见上课: allItems),但是我想分割数据,所以类项包含所有内容,包含保存subItems的项目中的公共List。

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.

此片段中发生的是在for循环期间,项目被添加了相同的值到日期列表,这是我在难以尝试将正确数量的条目添加到日期列表中给出多少次同一周数字在allStats列表中计算。

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;
}


推荐答案

听起来你可以做到这一点使用linq:

Sounds like you can do this with 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()
    };  

这篇关于拆分List&lt; T&gt;的部分。进入2 List&lt; T&gt;并加入那2个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆