【发布时间】:2012-01-17 03:59:25
【问题描述】:
我在表单上有一组文档“WineDocument”:
{
"Name": "Barbicato Morellino Di Scansano",
"Country": "Italy",
"Region": "Tuscany",
}
我需要进行查询以查找“国家/地区”字段的所有唯一值。我一直在尝试创建一个看起来像这样的索引:
class WineCountriesIndex: AbstractIndexCreationTask<WineDocument, string> {
public BeverageCountriesIndex() {
Map = wines => from wine in wines
where wine.Country != null
select new { Key = wine.Country };
Reduce = results => from result in results
group result by result into g
select new { Key = g.Key };
}
}
索引创建成功,我尝试使用以下代码:
IList<string> countries = session.Query<string, WineCountriesIndex>().ToList();
但这会产生 JsonSerializationException:“无法将 JSON 对象反序列化为类型 'System.String'。”。我猜这是因为 Json 解析器无法将 { Key = "Italy } 解析为字符串。但我不知道如何让 map/reduce 只返回一个字符串。
【问题讨论】:
-
wine.Country 是一个枚举?
-
不,它是一个字符串。它的可能值在编译时是未知的。
标签: c# json linq mapreduce ravendb