4

わかりました、これはちょっと厄介なクエリであり、私はそれで限られた成功しか収めていません。日時プロパティとその他のデータを持つFooクラスのリストがあります。私はほとんど毎分クラス/列を持っていますが、休日や週末のように欠席している日もあれば、丸一日欠席している日もあります。私の目標は、開始時刻から終了時刻までのすべての行について毎日グループ化することです。

ですから、ある日、私の開始時間は午前9時30分、終了時間は午後3時になるかもしれません。私が次のように処理するこの状況:

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時である場合があります。その場合、私はfooのグループを一晩中行きたいと思っています。そして、午後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 に答える