【发布时间】:2012-08-07 16:27:26
【问题描述】:
我必须创建一个查询以获取每年/每月发布的统计信息,例如按日期分组。我创建了一个索引:
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)
};
}
}
在工作室中,我有下一个 map 和 reduce 函数:
地图:
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"
}
谁能帮我解决我的代码问题?谢谢!
【问题讨论】: