linq group在DateTime列的开始时间到结束时间

时间:2009-05-14 03:45:12

标签: c# linq datetime group-by linq-to-objects

好的这是一个混乱的查询,我只是在它有限的成功。我有一个Foo类的列表,它具有datetime属性和其他数据。我每分钟都有一个班级/排,有些人失踪,整天都像假期或周末一样失踪。我的目标是将每天从开始时间到结束时间分组。

所以有些日子我的开始时间可能是上午9:30,下午3:00。我通过以下方式处理这种情况:

DateTime sd = new DateTime(2000, 1, 1, 9, 30, 0);
DateTime ed = new DateTime(2000, 1, 1, 15, 0, 0);
var z = Foos.Where(a=> a.FooDate.TimeOfDay >= sd.TimeOfDay &&
    a.FooDate.TimeOfDay < ed.TimeOfDay)
    .GroupBy(a=>a.FooDate.Date);

这很好用。我的问题有时候我的开始时间是晚上9点,结束时间是早上6点。在那种情况下,我希望这组foos过夜,如果晚上9点是星期五,并且在下一个星期一之前没有排,我希望小组能够度过周末。我甚至会对一个查询的建议感到满意,这个查询总是会在第二天进行。

我希望这清楚并欣赏任何想法。我尝试了很多其他的方法来做循环和创建另一个不同的日期列表,但我不满意。

2 个答案:

答案 0 :(得分:3)

在物理学中,当遇到相对问题时,他们可以选择零位。我们也是。

// time range expressed as an example in "absolute" terms
DateTime sd = new DateTime(2000, 1, 1, 9, 30, 0);
DateTime ed = new DateTime(2000, 1, 2, 6, 0, 0);

// time range expressed as a zero and range - "relative" terms
TimeSpan zero = sd.TimeOfDay;
TimeSpan range = ed.Subtract(sd);

 //the inputs
List<DateTime> myList = new List<DateTime>()
{
  new DateTime(2009, 1, 1, 10, 0, 0),  //group1
  new DateTime(2009, 1, 1, 17, 0, 0),  //group1
  new DateTime(2009, 1, 2, 9, 0, 0),  //this is filtered
  new DateTime(2009, 1, 2, 10, 0, 0),  //group2
  new DateTime(2009, 1, 2, 15, 0, 0),  //group2
  new DateTime(2009, 1, 3, 3, 0, 0),   //group2
  new DateTime(2009, 1, 3, 7, 0, 0),  //this is filtered
  new DateTime(2009, 1, 3, 10, 0, 0)   //group3
};

  //at last, the query.
var query = myList
  .Where(d => d.Subtract(zero).TimeOfDay < range)
  .GroupBy(d => d.Subtract(zero).Date);

 //  output the results
foreach (var g in query)
{
  Console.WriteLine("{0}", g.Count());
  foreach (var d in g)
  {
    Console.WriteLine("  {0}", d);
  }
}

结果:

2
  1/1/2009 10:00:00 AM
  1/1/2009 5:00:00 PM
3
  1/2/2009 10:00:00 AM
  1/2/2009 3:00:00 PM
  1/3/2009 3:00:00 AM
1
  1/3/2009 10:00:00 AM

答案 1 :(得分:0)

这样可行,但它可能不比你正在做的更漂亮

    private List<groupFoo> groupFoos(List<foo> foos)
    {

        //Group by Day into groupFoo
        var z = foos.GroupBy(a => a.FooDate.ToShortDateString()).Select(x => new groupFoo() { Key = x.Key, Start = x.First().FooDate, End = x.Last().FooDate }).ToList();


        //Create new list to hold groups
        var groupedFoos = new List<groupFoo>();

        //add all the good groups to the list
        groupedFoos.AddRange(z.FindAll(zz => zz.Start.CompareTo(zz.End) != 0));

        //Remove all of the good groups from the orignal list
        groupedFoos.ForEach(gf => z.RemoveAt(z.IndexOf(gf)));

        //Sort whats left
        z.Sort((a, b) => { return a.Start.CompareTo(b.Start); });

        while (z.Count > 1)
        {
            //grab the first 2 
            var y = z.Take(2);

            //create a new group foo and add it to the good list
            groupedFoos.Add(y.Aggregate((a, b) => new groupFoo() { Key = a.Key, Start = a.Start, End = b.End }));

            //remove the bad ones
            y.ToList().ForEach(yy => z.RemoveAt(z.IndexOf(yy)));


        }

        return groupedFoos;
    }

和groupFoo看起来像这样

public class groupFoo
{

    public string Key { get; set; }
    public DateTime Start { get; set; }
    public DateTime End { get; set; }

}

我使用的样本

        List<foo> foos = new List<foo>();

        foos.Add(new foo() { FooDate = new DateTime(2009, 1, 1, 9, 0, 0) });
        foos.Add(new foo() { FooDate = new DateTime(2009, 1, 1, 17, 0, 0) });
        foos.Add(new foo() { FooDate = new DateTime(2009, 1, 2, 9, 30, 0) });
        foos.Add(new foo() { FooDate = new DateTime(2009, 1, 3, 6, 0, 0) });
        foos.Add(new foo() { FooDate = new DateTime(2009, 1, 4, 9, 0, 0) });
        foos.Add(new foo() { FooDate = new DateTime(2009, 1, 4, 21, 0, 0) });