2015-06-15 50 views
1

我有Example类元素的列表:集团通过日期,时间和ID在一个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> TimeGroup { get; set; } 
} 

public class GroupedByTime 
{ 
    public List<GroupedById> IdGroup { get; set; } 
} 

public class GroupedById 
{ 
    public string Name { get; set; } 
} 

因此,结果是一个List<GroupedByDay>类型的列表,其中Examples按天,小时(时间跨度?)分组,最后由ID分组。

任何人都可以帮助我吗?

[编辑]
这就是我试图通过组ID的,但我想我应该从其他方面也许开始?

var result = 
    examples 
     .GroupBy(e => e.Id, e => new GroupedById 
     { 
      Name = e.Name 
     }); 
+1

你尝试什么Linq的代码? –

+0

检查我的编辑plz,我添加了一些失踪的东西。问题是我有一些日常任务。我想把这些任务分组几天(因为我想每天都要展示给他们一整天)。现在每天我都要在相同的时间分组。有些任务可以是相同的,我不想让他们重复。 – Nickon

+0

一如既往...没有帮助,只有-1 ... – Nickon

回答

3

如果你只是想组显示的目的,你不需要类GroupedByDayGroupedByTimeGroupedById

考虑examples是一种IEnumerable<Example>

var groupedExamples = from example in examples 
     group example by new { 
           example.SomeDate.Date, //Day 
           example.SomeDate.Hour, // Hour 
           example.Id // Id 
     } into g 
     select g; 

那么你就必须具有所需分组的IEnumerable<IGrouping<,Example>>

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

我平时写这些代码

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


     } 
+0

与OP的问题有什么关系? – Soader03