C# 在一个LINQ中按天、时间和id分组
我有一个C# 在一个LINQ中按天、时间和id分组,c#,linq,C#,Linq,我有一个示例类元素列表: public class Example { public long Id { get; set; } public string Name { get; set; } public DateTime SomeDate { get; set; } } 现在,我想使用ONELINQ对其进行分组,以形成以下层次结构: public class GroupedByDay { public List<GroupedByTime> TimeG
示例类元素列表:
public class Example
{
public long Id { get; set; }
public string Name { get; set; }
public DateTime SomeDate { get; set; }
}
现在,我想使用ONELINQ
对其进行分组,以形成以下层次结构:
public class GroupedByDay
{
public List<GroupedByTime> TimeGroup { get; set; }
}
public class GroupedByTime
{
public List<GroupedById> IdGroup { get; set; }
}
public class GroupedById
{
public string Name { get; set; }
}
如果只是为了显示而分组,则不需要类GroupedByDay
、GroupedByTime
和GroupedById
考虑到示例
是一个IEnumerable
然后您将拥有一个带有所需分组的IEnumerable
:
foreach(var g in groupedExample){
Console.WriteLine(String.Format("Day {0} at hour {1} with id {2}", g.Key.Date, g.Key.Hour, g.Key.Id));
foreach(var example in g)
Console.WriteLine(" - " + example.Name);
}
如果只是为了显示而分组,则不需要类GroupedByDay
、GroupedByTime
和GroupedById
考虑到示例
是一个IEnumerable
然后您将拥有一个带有所需分组的IEnumerable
:
foreach(var g in groupedExample){
Console.WriteLine(String.Format("Day {0} at hour {1} with id {2}", g.Key.Date, g.Key.Hour, g.Key.Id));
foreach(var example in g)
Console.WriteLine(" - " + example.Name);
}
我通常写这些代码
public static DateTime GetDateByWeekDay(DateTime startDate, int week, Int32 day)
{
int Year = Getyear(startDate);
DateTime jan1 = new DateTime(Year, 1, 1);
int daysOffset = DayOfWeek.Monday - jan1.DayOfWeek;
DateTime firstMonday = jan1.AddDays(daysOffset);
var cal = CultureInfo.CurrentCulture.Calendar;
int firstWeek = cal.GetWeekOfYear(firstMonday, CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday);
var weekNum = week;
if (firstWeek <= 1)
{
weekNum -= 1;}
var result = firstMonday.AddDays(weekNum * 7);
return result.AddDays(day);
}
publicstaticdatetime GetDateByWeekDay(DateTime startDate,intweek,Int32天)
{
int Year=Getyear(起始日期);
DateTime jan1=新的日期时间(年,1,1);
int daysofset=DayOfWeek.Monday-jan1.DayOfWeek;
DateTime firstMonday=1月1日。添加天数(Daysofset);
var cal=CultureInfo.CurrentCulture.Calendar;
int firstWeek=cal.GetWeekOfYear(firstMonday,CalendarWeekRule.FirstFourDayWeek,DayOfWeek.Monday);
var weekNum=周;
如果(第一周我通常写这些代码
public static DateTime GetDateByWeekDay(DateTime startDate, int week, Int32 day)
{
int Year = Getyear(startDate);
DateTime jan1 = new DateTime(Year, 1, 1);
int daysOffset = DayOfWeek.Monday - jan1.DayOfWeek;
DateTime firstMonday = jan1.AddDays(daysOffset);
var cal = CultureInfo.CurrentCulture.Calendar;
int firstWeek = cal.GetWeekOfYear(firstMonday, CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday);
var weekNum = week;
if (firstWeek <= 1)
{
weekNum -= 1;}
var result = firstMonday.AddDays(weekNum * 7);
return result.AddDays(day);
}
publicstaticdatetime GetDateByWeekDay(DateTime startDate,intweek,Int32天)
{
int Year=Getyear(起始日期);
DateTime jan1=新的日期时间(年,1,1);
int daysofset=DayOfWeek.Monday-jan1.DayOfWeek;
DateTime firstMonday=1月1日。添加天数(Daysofset);
var cal=CultureInfo.CurrentCulture.Calendar;
int firstWeek=cal.GetWeekOfYear(firstMonday,CalendarWeekRule.FirstFourDayWeek,DayOfWeek.Monday);
var weekNum=周;
如果(第一周你尝试了什么Linq代码?请检查我的编辑,我添加了一些缺少的内容。问题是我有一些日常任务。我想按天对这些任务进行分组(因为我想一周一天地显示它们)现在,在每天的工作时间内,我想按相同的时间对它进行分组。有些任务可能是相同的,我不想重复显示它们。而且一如既往地…没有帮助,只是-1…类GroupedByDay
,GroupedByTime
和GroupedById
真的很有必要吗?你不能有一个IEnumerable
?嗯,但是如果我有IEn吗numerable
那么如何同时按天分组和按小时分组?可能我有什么地方错了;您尝试了什么Linq代码?请检查我的编辑,我添加了一些缺少的内容。问题是我有一些日常任务。我想按天分组这些任务(因为我想一周一天地显示它们)现在,在每天的工作时间内,我想按相同的时间对它进行分组。有些任务可能是相同的,我不想重复显示它们。而且一如既往地…没有帮助,只是-1…类GroupedByDay
,GroupedByTime
和GroupedById
真的很有必要吗?你不能有一个IEnumerable
?嗯,但是如果我有IEn吗numerable
那么如何同时按天分组和按小时分组?可能我有什么地方错了;您尝试了什么Linq代码?请检查我的编辑,我添加了一些缺少的内容。问题是我有一些日常任务。我想按天分组这些任务(因为我想一周一天地显示它们)现在,在每天的工作时间内,我想按相同的时间对它进行分组。有些任务可能是相同的,我不想重复显示它们。而且一如既往地…没有帮助,只是-1…类GroupedByDay
,GroupedByTime
和GroupedById
真的很有必要吗?你不能有一个IEnumerable
?嗯,但是如果我有IEn吗无数
那么如何同时按天分组和按小时分组?也许我有点错了;这与OP的问题有什么关系?与OP的问题有什么关系?与OP的问题有什么关系?