gpt4 book ai didi

c# - 来自 List 的数据拆分为另一个具有 2 个嵌套 List 的 List

转载 作者:太空宇宙 更新时间:2023-11-03 20:18:07 24 4
gpt4 key购买 nike

我在使用这个 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

EDIT: I am sorry, I forgot to mention that I've tried the .ToList() method at the end instead of the cast at the start which produces this error:

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();

最佳答案

问题是你的 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()

我们对您要实现的目标的了解还不够。

关于c# - 来自 List<T> 的数据拆分为另一个具有 2 个嵌套 List<T> 的 List<T>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15339547/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com