C# IList在多映射/减少结果中?

C# IList在多映射/减少结果中?,c#,mapreduce,ravendb,C#,Mapreduce,Ravendb,我正在与RavenDB的多映射/减少概念斗争,最近我被问及如何正确编写多映射/减少索引 我在那个问题上得到了简单的索引,但当我试图使它更复杂一点时,我无法使它工作。我想做的是使索引的结果包含字符串列表,即: class RootDocument { public string Id { get; set; } public string Foo { get; set; } public string Bar { get; set; } public IList<string

我正在与RavenDB的多映射/减少概念斗争,最近我被问及如何正确编写多映射/减少索引

我在那个问题上得到了简单的索引,但当我试图使它更复杂一点时,我无法使它工作。我想做的是使索引的结果包含字符串列表,即:

class RootDocument {
  public string Id { get; set; }
  public string Foo { get; set; }
  public string Bar { get; set; }
  public IList<string> Items { get; set; }
}

public class ChildDocument {
  public string Id { get; set; }
  public string RootId { get; set; }
  public int Value { get; set; }
}

class RootsByIdIndex: AbstractMultiMapIndexCreationTask<RootsByIdIndex.Result> {
  public class Result {
    public string Id { get; set; }
    public string Foo { get; set; }
    public string Bar { get; set; }
    public IList<string> Items { get; set; }
    public int Value { get; set; }
  }

  public RootsByIdIndex() {
    AddMap<ChildDocument>(
      children => from child in children
        select new {
          Id = child.RootId,
          Foo = (string)null,
          Bar = (string)null,
          Items = default(IList<string>),
          Value = child.Value
      });
      AddMap<RootDocument>(
        roots => from root in roots
          select new {
           Id = root.Id,
           Foo = root.Foo,
           Bar = root.Bar,
           Items = root.Items,
           Value = 0
        });
      Reduce = 
        results => from result in results
          group result by result.Id into g
            select new {
              Id = g.Key,
              Foo = g.Select(x => x.Foo).Where(x => x != null).FirstOrDefault(),
              Bar = g.Select(x => x.Bar).Where(x => x != null).FirstOrDefault(),
              Items = g.Select(x => x.Items).Where(
                x => x != default(IList<string>).FirstOrDefault(),
                Value = g.Sum(x => x.Value)
              };
    }
}
基本上,在映射ChildDocuments和RootDocument的Items属性值时,我尝试将Items属性设置为defaultIList。然而,这不起作用。它给出了错误消息

请求时出错无法理解查询:

-第2行第285列:表达式无效

-第2行第324列:无法分析double.0.0


上传索引时。如何处理多重映射/减少索引中的列表?

David,您需要了解RavenDB将其索引存储在Lucene.NET中。这意味着,索引中不能有任何复杂的.net类型

在您的示例中,我建议您使用简单字符串而不是IList。然后可以加入字符串项:

AddMap<ChildDocument>(
  children => from child in children
    select new {
      Id = child.RootId,
      Foo = (string)null,
      Bar = (string)null,
      Items = (string)null,
      Value = child.Value
  });
  AddMap<RootDocument>(
    roots => from root in roots
      select new {
       Id = root.Id,
       Foo = root.Foo,
       Bar = root.Bar,
       Items = string.Join(";", root.Items),
       Value = 0
    });

David,您需要了解RavenDB将其索引存储在Lucene.NET中。这意味着,索引中不能有任何复杂的.net类型

在您的示例中,我建议您使用简单字符串而不是IList。然后可以加入字符串项:

AddMap<ChildDocument>(
  children => from child in children
    select new {
      Id = child.RootId,
      Foo = (string)null,
      Bar = (string)null,
      Items = (string)null,
      Value = child.Value
  });
  AddMap<RootDocument>(
    roots => from root in roots
      select new {
       Id = root.Id,
       Foo = root.Foo,
       Bar = root.Bar,
       Items = string.Join(";", root.Items),
       Value = 0
    });

不要在索引中使用列表,而是使用数组

AddMap<ChildDocument>(
  children => from child in children
    select new {
      Id = child.RootId,
      Foo = (string)null,
      Bar = (string)null,
      Items = new string[0],
      Value = child.Value
  });
  AddMap<RootDocument>(
    roots => from root in roots
      select new {
       Id = root.Id,
       Foo = root.Foo,
       Bar = root.Bar,
       Items = root.Items,
       Value = 0
    });

Reduce = 
    results => from result in results
      group result by result.Id into g
        select new {
          Id = g.Key,
          Foo = g.Select(x => x.Foo).Where(x => x != null).FirstOrDefault(),
          Bar = g.Select(x => x.Bar).Where(x => x != null).FirstOrDefault(),
          Items = g.SelectMany(x=>x.Items),
          Value = g.Sum(x => x.Value)
          };

不要在索引中使用列表,而是使用数组

AddMap<ChildDocument>(
  children => from child in children
    select new {
      Id = child.RootId,
      Foo = (string)null,
      Bar = (string)null,
      Items = new string[0],
      Value = child.Value
  });
  AddMap<RootDocument>(
    roots => from root in roots
      select new {
       Id = root.Id,
       Foo = root.Foo,
       Bar = root.Bar,
       Items = root.Items,
       Value = 0
    });

Reduce = 
    results => from result in results
      group result by result.Id into g
        select new {
          Id = g.Key,
          Foo = g.Select(x => x.Foo).Where(x => x != null).FirstOrDefault(),
          Bar = g.Select(x => x.Bar).Where(x => x != null).FirstOrDefault(),
          Items = g.SelectMany(x=>x.Items),
          Value = g.Sum(x => x.Value)
          };

好的,我只是假设我可以索引任何我可以存储的东西,并且存储一个IList工作正常。好的,我只是假设我可以索引任何我可以存储的东西,并且存储一个IList工作正常。