1
我必须创建一个查询来获得每年/每月发布的统计信息,例如,按日期分组。我创建一个索引:RavenDB地图/按日期分组减少
public class Posts_Count : AbstractIndexCreationTask<Post, ArchiveItem>
{
public Posts_Count()
{
Map = posts => from post in posts
select new
{
Year = post.PublishedOn.Year,
Month = post.PublishedOn.Month,
Count = 1
};
Reduce = results => from result in results
group result by new {
result.Year,
result.Month
}
into agg
select new
{
Year = agg.Key.Year,
Month = agg.Key.Month,
Count = agg.Sum(x => x.Count)
};
}
}
在录音室我有一张地图和减少功能:
地图:
docs.Posts.Select(post => new {Year = post.PublishedOn.Year, Month = post.PublishedOn.Month, Count = 1})
减少:
results
.GroupBy(result => new {Year = result.Year, Month = result.Month})
.Select(agg => new {Year = agg.Key.Year, Month = agg.Key.Month, Count = agg.Sum(x => ((System.Int32)(x.Count)))})
但问题是我送花儿给人得到年份和月份属性的空值:
{
"Year": null,
"Month": null,
"Count": "1"
}
任何人都可以帮我解决我的代码问题吗?谢谢!
嗯,上面的索引看起来不错,但我们不应该为每个项目定义SortOption吗?如'Sort(x => x.Year,SortOptions.Int);排序(x => x.Month,SortOptions.Int);排序(x => x.Count,SortOptions.Int);'?否则,我们不会得到不一致的排序结果吗? – tugberk 2014-01-14 13:19:23
@tugberk - 是的,这是正确的。 – 2014-01-14 15:32:04