2009-05-14 18 views
4

これはちょっと面倒なクエリです。私はdatetimeプロパティと他のデータを持つFooクラスのリストを持っています。休日や週末のように行方不明と一部の行方不明があり、毎分クラス/行があります。私の目標は、開始時刻から終了時刻までのすべての行を毎日グループ化することです。linqグループの開始時刻から終了時刻までのDateTime列

私の開始時間は、午前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時までです。その場合、私は一晩に行くfoosのグループが欲しい、そして午後9時は金曜日にあり、次の月曜日まで私はグループが週末にまたがることを望むまで行がありません。私は昨日に行くつもりのクエリの提案にも満足しています。

私はそのことを明確にしていただければ幸いです。私はループでこれを行う他の方法をたくさん試して、異なる日付などの別のリストを作成しましたが、それに満足していません。

+0

私はあなたが決定しようとしているものに関して、混乱しています。エントリ間の時間を取得しようとしていますか(Foos)?または2回の間にエントリの数を数えていますか? – Jeremy

+0

fooオブジェクトのリストを時間範囲にグループ化しようとしています。したがって、最初の日には、その日の開始時刻から翌日にグループ化するデータがあり、データの終了時刻があります。その後、その日に開始時に再び開始し、続行します。 – RBear

答えて

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 
+0

WOW!これは私が探しているものです。それは私の定期的なケースでも動作します。大変ありがとう! – RBear

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) }); 
関連する問題