用户选择的日期范围,以周通过linq等
本文关键字:周通过 linq 范围 选择 日期 用户 | 更新日期: 2023-09-27 18:14:30
我有一个表单,用户选择开始和结束日期来获取数据。虽然用户选择开始和结束日期,但我必须在日期范围内按周显示表中的数据。
我的模型很简单
public class DateBetween
public Datetime StartDate{ get;set;}
public Datetime EndDate{ get;set;}
我从数据库
中获得这些日期之间的数据列表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
您可以使用手写的LINQ或使用以下答案:
LINQ查询将有序列表按一定条件拆分为连续点的子列表
示例:只使用Linq和DateTime/Calendar:
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())
诀窍是获得任何给定年份中每个星期的确切日历开始日期,为此我循环使用了DateTime的Office Automation方法。这样做产生了一个有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日至2011年1月15日期间,有22个文件被修改
在12/06/2011和18/06/2011之间有11个文件被修改
等等。
首先选择带有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();