我有一个存储在我的对象中的日期集合。这是样本数据。实时,日期将来自服务电话,我将不知道将返回多少日期和数量:
var ListHeader = new List<ListHeaderData>
{
new ListHeaderData
{
EntryDate = new DateTime(2013, 8, 26)
},
new ListHeaderData
{
EntryDate = new DateTime(2013, 9, 11)
},
new ListHeaderData
{
EntryDate = new DateTime(2013, 1, 1)
},
new ListHeaderData
{
EntryDate = new DateTime(2013, 9, 15)
},
new ListHeaderData
{
EntryDate = new DateTime(2013, 9, 17)
},
new ListHeaderData
{
EntryDate = new DateTime(2013, 9, 5)
},
};
我现在需要按照日期范围进行分组:
Today (1) <- contains the date 9/17/2013 and count of 1
within 2 weeks (3) <- contains dates 9/15,9/11,9/5 and count of 3
More than 2 weeks (2) <- contains dates 8/26, 1/1 and count of 2
这是我的LINQ声明,它没有实现我的需要,但我认为我在球场(如果我不是那么善良):
var defaultGroups = from l in ListHeader
group l by l.EntryDate into g
orderby g.Min(x => x.EntryDate)
select new { GroupBy = g };
按个别日期分组,所以我有6个组,每个组有1个日期。如何按每个组中的日期范围,计数和排序进行分组?提前感谢。
答案 0 :(得分:5)
这样做怎么样?
引入一个新属性进行分组和分组。
class ListHeaderData
{
public DateTime EntryDate;
public int DateDifferenceFromToday
{
get
{
TimeSpan difference = DateTime.Today - EntryDate.Date;
if (difference.TotalDays == 0)//today
{
return 1;
}
else if (difference.TotalDays <= 14)//less than 2 weeks
{
return 2;
}
else
{
return 3;//something else
}
}
}
}
编辑:正如@servy在评论中指出的那样,其他开发人员可能会使用int
混淆enum
将更具可读性。
所以,你的类的修改版本看起来像这样
class ListHeaderData
{
public DateTime EntryDate;
public DateRange DateDifferenceFromToday
{
get
{
//I think for this version no comments needed names are self explanatory
TimeSpan difference = DateTime.Today - EntryDate.Date;
if (difference.TotalDays == 0)
{
return DateRange.Today;
}
else if (difference.TotalDays <= 14)
{
return DateRange.LessThanTwoWeeks;
}
else
{
return DateRange.MoreThanTwoWeeks;
}
}
}
}
enum DateRange
{
None = 0,
Today = 1,
LessThanTwoWeeks = 2,
MoreThanTwoWeeks = 3
}
并像这样使用
var defaultGroups = from l in ListHeader
group l by l.DateDifferenceFromToday into g // <--Note group by DateDifferenceFromToday
orderby g.Min(x => x.EntryDate)
select new { GroupBy = g };
答案 1 :(得分:5)
介绍数组,其中包含您要分组的范围。这是两个范围 - 今天(零天)和14天(两周):
var today = DateTime.Today;
var ranges = new List<int?> { 0, 14 };
现在按照它所属的范围对您的项目进行分组。如果没有合适的范围(所有日期超过两周),则将使用默认的null
范围值:
var defaultGroups =
from h in ListHeader
let daysFromToday = (int)(today - h.EntryDate).TotalDays
group h by ranges.FirstOrDefault(range => daysFromToday <= range) into g
orderby g.Min(x => x.EntryDate)
select g;
更新:添加自定义范围以进行分组:
var ranges = new List<int?>();
ranges.Add(0); // today
ranges.Add(7*2); // two weeks
ranges.Add(DateTime.Today.Day); // within current month
ranges.Add(DateTime.Today.DayOfYear); // within current year
ranges.Sort();
答案 2 :(得分:1)
您是否特意想以这种方式实现解决方案?您是否真的想在课堂上引入虚假属性以满足这些要求?
这三行将满足您的要求,对于大型集合来说,性能会更高。
var todays = listHeader.Where(item => item.EntryDate == DateTime.Today);
var twoWeeks = listHeader.Where(item => item.EntryDate < DateTime.Today.AddDays(-1)
&& item.EntryDate >= DateTime.Today.AddDays(-14));
var later = listHeader.Where(item => item.EntryDate < DateTime.Today.AddDays(-14));
您也可以在不影响课程的情况下获得不同分组的灵活性。
[编辑:响应订购查询]
使用上面提供的Enum,您可以应用Union子句和OrderBy子句Linq扩展方法,如下所示:
var ord = todays.Select(item => new {Group = DateRange.Today, item.EntryDate})
.Union(
twoWeeks.Select(item => new {Group = DateRange.LessThanTwoWeeks, item.EntryDate}))
.Union(
later.Select(item => new {Group = DateRange.MoreThanTwoWeeks, item.EntryDate}))
.OrderBy(item => item.Group);
请注意,我正在通过Linq Select和匿名类添加分组,以再次动态推送Group属性而不影响原始类。这将根据原始帖子生成以下输出:
Group EntryDate Today 17/09/2013 00:00:00 LessThanTwoWeeks 11/09/2013 00:00:00 LessThanTwoWeeks 15/09/2013 00:00:00 LessThanTwoWeeks 05/09/2013 00:00:00 MoreThanTwoWeeks 26/08/2013 00:00:00 MoreThanTwoWeeks 01/01/2013 00:00:00
并获取带有计数的分组日期范围:
var ord = todays.Select(item => new {Group = DateRange.Today, Count=todays.Count()})
.Union(
twoWeeks.Select(item => new {Group = DateRange.LessThanTwoWeeks, Count=twoWeeks.Count()}))
.Union(
later.Select(item => new {Group = DateRange.MoreThanTwoWeeks, Count=later.Count()}))
.OrderBy(item => item.Group);
输出是:
Group Count Today 1 LessThanTwoWeeks 3 MoreThanTwoWeeks 2
答案 3 :(得分:0)
我想这取决于你计划使用它的程度。我生成了很多报告,所以我创建了一个模型IncrementDateRange
,StartTime
,EndTime
和TimeIncrement
作为枚举。
时间增量处理程序有很多基于开关的功能,根据小时/天/周/月/季度/年等等,在开始和结束范围之间吐出一系列时间。
然后你得到IncrementDateRange
的列表和linq中的任何一个:
TotalsList = times.Select(t => new RetailSalesTotalsListItem()
{
IncrementDateRange = t,
Total = storeSales.Where(s => s.DatePlaced >= t.StartTime && s.DatePlaced <= t.EndTime).Sum(s => s.Subtotal),
})
或
TotalsList = storeSales.GroupBy(g => g.IncrementDateRange.StartTime).Select(gg => new RetailSalesTotalsListItem()
{
IncrementDateRange = times.First(t => t.StartTime == gg.Key),
Total = gg.Sum(rs => rs.Subtotal),
}).ToList(),