【问题标题】:RavenDB Map Reduce Transform fold a distinct list of stringsRavenDB Map Reduce Transform 折叠一个不同的字符串列表
【发布时间】:2013-01-07 13:27:55
【问题描述】:

我的问题很短,我坚持下去。我有一系列文件,例如:

{
...,
"Type": [
    "Type1",
    "Type2",
    "Type3",
    "Type4"
  ],
...
}

而且我认为我需要创建一个 map/reduce 索引来将这些值分组到一个字符串列表中,例如,如果我有这两个文档:

{
...,
"Type": [
    "Type1",
    "Type3"
  ],
...
}

{
...
"Type": [
    "Type2",
    "Type3",
    "Type4"
  ],
...
}

我需要一个包含所有不同值的列表的结果,例如 {"Type1","Type2","Type3","Type4"}

我不知道怎么做,我尝试了好几种方法都没有成功。

我忘了说,我有大量的数据,现在有1500多个文档

public class ProjectTypeIndex : AbstractIndexCreationTask<Project, ProjectTypeIndex.ReduceResult>
{
    public class ReduceResult
    {
        public string ProjectType { get; set; }
    }

    public ProjectTypeIndex()
    {
        Map = projects => from project in projects
                                   select new
                                       {
                                           project.Type
                                       };
        Reduce = results => from result in results
                            group result by result.Type into g 
                            select new
                                {
                                    ProjectType = g.Key
                                };
    }
}

【问题讨论】:

  • 您只想要不同的值吗?您需要按某事对其进行分组吗?
  • 没错,我只想将类型列表中的不同值检索到文档中。我将用我制作的一些测试代码更新我的问题。感谢您的快速回答。

标签: c# linq mapreduce ravendb


【解决方案1】:

大家好,我找到了一个解决方案,但我不知道是否是最好的方法,因为我仍然没有字符串列表,只有结果,我解决了它通过 foreach 填充字符串列表,这里是索引创建。如果有人可以检查代码,我将非常感激。

public class ProjectTypeIndex : AbstractIndexCreationTask<Project, ProjectTypeIndex.ReduceResult>
{
    public class ReduceResult
    {
        public string ProjectType { get; set; }
    }

    public ProjectTypeIndex()
    {
        Map = projects => from project in projects
                                   from type in project.Type
                                   select new
                                       {
                                           ProjectType = type
                                       };

        Reduce = results => from result in results
                            group result by result.ProjectType into g
                            select new
                                {
                                    ProjectType = g.Key
                                };
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-18
    相关资源
    最近更新 更多