来自List <t>的数据拆分为另一个List <t>,其中包含2个嵌套List <t> </t> </t> </t>

时间:2013-03-11 13:14:18

标签: c# linq list

我对这个linq查询遇到了一些麻烦:

我有一个填充数据的列表(allStats)(参见allItems类) 该列表包含我需要分解的不同日期的几个条目,因此它们按月和周数分开:

    MonthNumber: 1,
    List<Weeks> Weeks:
        WeekNumber: 1,
        List<Days> Days:
            PunchedInLate: true,
            PunchedOutLate: false,
            PunchInDate: 2013-1-1 08:20:10,
            PunchOutDate: 2013-1-1 15:00:00
            PunchedInLate: true,
            PunchedOutLate: false,
            PunchInDate: 2013-1-2 08:20:10,
            PunchOutDate: 2013-1-2 15:00:00
            ...
            PunchedInLate: true,
            PunchedOutLate: false,
            PunchInDate: 2013-1-5 08:20:10,
            PunchOutDate: 2013-1-5 15:00:00
    MonthNumber: 1,
    List<Weeks> Weeks:
        WeekNumber: 2,
        List<Days> Days:
            PunchedInLate: true,
            PunchedOutLate: false,
            PunchInDate: 2013-1-10 08:20:10,
            PunchOutDate: 2013-1-10 15:00:00
            PunchedInLate: true,
            PunchedOutLate: false,
            PunchInDate: 2013-1-12 08:20:10,
            PunchOutDate: 2013-1-12 15:00:00

PasteBin - Here you can download a sample program so you can run it from your machine

它本质上是我试图创建的这个问题答案的补充: SO - Splitting parts of a List into 2 List's and joining those 2

  

编辑:我很抱歉,我忘了提到我在最后尝试了.ToList()方法,而不是在开头产生此错误的演员:

Cannot implicitly convert type 'System.Collections.Generic.List<System.Collections.Generic.IEnumerable<TestClass.Weeks>>' to 'System.Collections.Generic.List<TestClass.Weeks>'

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 Months
{
    public int MonthNumber { get; set; }
    public List<Weeks> Weeks { get; set; }
}
public class Weeks
{
    public int WeekNumber { get; set; }
    public List<Days> Days { get; set; }
}
public class Days
{
    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; } 
}

代码:

List<allItems> allStats = getAllStats(userId);
List<Months> stats = new List<Months>();
var asItems =
    from item in allStats
    group item by new { month = item.MonthNumber } into Month
    select new Months()
    {
        MonthNumber = Month.Key.month,
        Weeks = Month.Select(week =>
            from weeks in allStats
            group weeks by new { week = weeks.WeekNumber } into Week
            select new Weeks()
            {
                //WeekNumber = week.WeekNumber,
                WeekNumber = Week.Key.week, // I just noticed that I guess that I 
                                            // need this here, so I can group the 
                Days = Month.Select(days => // days correctly, right?
                    new Days()
                    {
                        PunchedInLate = days.PunchedInLate,
                        PunchedOutLate = days.PunchedOutLate,
                        DayOfWeek = days.DayOfWeek,
                        PunchInDate = days.PunchInDate,
                        PunchOutDate = days.PunchOutDate
                    }).ToList()
            }).ToList()
    };
List<Months> stat = asItems.ToList();

3 个答案:

答案 0 :(得分:4)

问题是您的Month.Select(...) 没有返回List<Weeks>。您可以删除演员表并使用:

Week = Month.Select(week => 
    ... code as before ...
).ToList()
编辑:好的,我知道还有什么问题。对于每个week,您使用的是一个生成多个 Weeks个对象的查询。所以这一部分:

from weeks in allStats
...
select new Weeks() { ... }

属于IEnumerable<Weeks>()类型 - 并且它被用作Month.Select(week => ...)中投影的主体,因此您有一系列序列。目前尚不清楚如何将其转换为单个列表。例如,您可以使用:

Week = Month.Select(week => 
    ... code as before ...
).First().ToList()

或:

Week = Month.Select(week => 
    ... code as before ...
).SelectMany(x => x).ToList()

我们根本不了解你想要达到的目标。

答案 1 :(得分:1)

在我看来,你有一个问题,就是你正在施展:

List<allItems> allStats = getAllStats(userId);
List<Months> stats = new List<Months>();
var asItems =
    from item in allStats
    group item by new { month = item.MonthNumber } into Month
    select new Months()
    {
        MonthNumber = Month.Key.month,
        Weeks = Month.Select(week => //Don't cast here, put a ToList() at the end.
            from weeks in allStats
            group weeks by new { week = weeks.WeekNumber } into Week
            select new Weeks()
            {
                WeekNumber = week.WeekNumber,
                Days = Month.Select(days =>
                    new Days()
                    {
                        PunchedInLate = days.PunchedInLate,
                        PunchedOutLate = days.PunchedOutLate,
                        DayOfWeek = days.DayOfWeek,
                        PunchInDate = days.PunchInDate,
                        PunchOutDate = days.PunchOutDate
                    }).ToList()
            }).ToList(); //*** ToList() added here ***
    };
List<Months> stat = asItems.ToList();

答案 2 :(得分:0)

在您选择“月份”后,您似乎错过了ToList

List<allItems> allStats = getAllStats(userId);
List<Months> stats = new List<Months>();
var asItems =
    from item in allStats
    group item by new { month = item.MonthNumber } into Month
    select new Months()
    {
        MonthNumber = Month.Key.month,
        Weeks = Month.Select(week =>
            from weeks in allStats
            group weeks by new { week = weeks.WeekNumber } into Week
            select new Weeks()
            {
                WeekNumber = week.WeekNumber,
                Days = Month.Select(days =>
                    new Days()
                    {
                        PunchedInLate = days.PunchedInLate,
                        PunchedOutLate = days.PunchedOutLate,
                        DayOfWeek = days.DayOfWeek,
                        PunchInDate = days.PunchInDate,
                        PunchOutDate = days.PunchOutDate
                    }).ToList()
            }).ToList()     // <-- here
    };
List<Months> stat = asItems.ToList();

请注意,在这种情况下你也不需要演员。