4

好的,这是一个混乱的查询,我只取得了有限的成功。我有一个包含 datetime 属性和其他数据的 Foo 类的列表。我几乎每分钟都有一堂课/一排,有些缺课,有些缺课,比如假期或周末。我的目标是每天对从开始时间到结束时间的所有行进行分组。

所以在某些日子里,我的开始时间可能是上午 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 点是星期五,并且直到下一个星期一之前没有行,我希望该组跨越周末。我什至会很高兴提出一个查询的建议,该查询总是会转到第二天。

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

4

2 回答 2

4

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

// 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
于 2009-05-14T17:30:13.617 回答
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) });
于 2009-05-14T16:57:31.857 回答