2011-11-13 67 views
2

我有一個表格,用戶選擇開始和結束日期來獲取數據。 儘管用戶選擇開始和結束日期,但我必須在日期範圍內逐周顯示錶中的數據。用戶通過linq等選擇日期範圍到幾周

我的模型很簡單

public class DateBetween 
     public Datetime StartDate{ get;set;} 
     public Datetime EndDate{ get;set;} 

我讓我的這些日期之間的DATAS的名單從數據庫

IList<Revenue> datas = DB.CreateCrateria(typeof(Revenue)) 
       .Add(Restrictions.Bt("Date", model.startDate, model.endDate)) 
       .List<Revenue>(); 

public class Revenue 
     public int Id{ get;set;} 
     public double Revenue { get;set;} 
     public Datetime RevenueDate{ get;set;} 

例子:

id  Date   Revenue 
1  10/11/2011 554 
2  11/10/2011 500 

如果用戶選擇約會像6/30/2011年和10/15/2011

我想展現給用戶

Week   Date      Avg.Revenue 
Week 1  6/30/2011-7/2/2011  587 
Week 2  7/3/2011-7/9/2011  650 

...

有任何建議做骨料本功能。在LINQ

回答

0

你可以使用手寫的LINQ或使用這樣的回答:

LINQ query to split an ordered list into sublists of contiguous points by some criteria

例子:僅僅使用普通的LINQ和日期時間/日曆其他:

var rnd = new Random(); 
var data = Enumerable.Range(1,100).Select(i => DateTime.Now.AddDays(rnd.Next(-91000,91000)/24)); 

var calendar = CultureInfo.CurrentCulture.Calendar; 
Func<DateTime, int> twoWeeks = dt => (dt.Year * 100) + 2 * (calendar.GetWeekOfYear(dt, CalendarWeekRule.FirstFullWeek, DayOfWeek.Sunday)/2); 

var by2weeks = data.GroupBy(twoWeeks); 

foreach (var period in by2weeks.OrderBy(g => g.Key)) 
{ 
    Console.WriteLine("{0}: {1}", period.Key, string.Join(", ", period)); 
} 

對於C#3.5和早期的string.Join(", ", period)

string.Join(", ", period.Select(o => o.ToString()).ToArray()) 
+0

主要問題是如何將日期範圍分組爲幾周並與其他數據庫操作進行聚合。 – gandil

+0

ahem,我只是做了一個樣本;你不是說你實際上是指LINQ-to-SQL或LINQ-to-EF?我不知道什麼與那些:(我希望我的分組方式可以幫助 – sehe

0

訣竅是獲取任何給定年份每週的確切日曆開始日期,並且爲此我打開了DateTime的Office自動化方法。這樣做產生了53個詞條的詞典。之後,所有的標準LINQ分組和引用到開始日期的字典中。

 Calendar cal = Calendar.ReadOnly(CultureInfo.CurrentCulture.Calendar); 
     StringBuilder sb = new StringBuilder(); 
     DirectoryInfo rdi = new DirectoryInfo(Root); // get all files in the root directory 
     List<FileInfo> allfis = rdi.GetFiles("*", SearchOption.AllDirectories).ToList(); 
     var a = allfis.GroupBy(q=>cal.GetYear(q.LastWriteTime)); 
     foreach (var b in a.Where(q=>q.Key==2011)) // this year only 
     { 
      double yearStartOaDate = new DateTime(b.Key, 1, 1).ToOADate(); 
      double yearEndOaDate = yearStartOaDate + 365; 
      // get exact start dates for each week 
      Dictionary<int, DateTime> weekStartingDates = new Dictionary<int, DateTime>(); 
      while (yearStartOaDate <= yearEndOaDate) 
      { 
       DateTime dt = DateTime.FromOADate(yearStartOaDate); 
       int ww = cal.GetWeekOfYear(dt, CalendarWeekRule.FirstDay, DayOfWeek.Sunday); 
       if(!weekStartingDates.ContainsKey(ww)) 
       { 
        weekStartingDates.Add(ww, dt); 
       } 
       yearStartOaDate += ww == 1 ? 1 : 7; 
      } 
      var c = b.GroupBy(q => cal.GetWeekOfYear(q.LastWriteTime, CalendarWeekRule.FirstDay, DayOfWeek.Sunday)).OrderBy(q=>q.Key); 
      foreach(var d in c) 
      { 
       sb.AppendLine("Between " + weekStartingDates[d.Key].ToShortDateString() + " and " + weekStartingDates[d.Key].AddDays(6).ToShortDateString() + " there were " + d.Count() + " files modified"); 
      } 
     } 
     File.WriteAllText("results.txt", sb.ToString()); 

,結果......

之間2011年9月1日和15/01/2011共有22個文件修改

之間12/06/2011和18/06/2011共有11個文件修改

等等等等

0

與WEEKOFYEAR然後組首先選擇日期對象,並...

var result = datas.Select(p => new 
    { 
     week = EntityFunctions.DiffDays(EntityFunctions.CreateDateTime(p.Date.Year, 1, 1, 0, 0, 0), p.Date.Value).Value/7, 
     Date= p.Date, 
     Revenue= p.Revenue 
    }).GroupBy(p => p.week) 
    .Select(p => new 
    { 
     week=p.Key, 
     Date=string.Format("{0:M/d/yyyy}",p.Min(q=>q.Date))+"-"+string.Format("{0:M/d/yyyy}",p.Max(q=>q.Date)) 
     Revenue=p.Average(q=>q.Revenue) 
    }).ToList();