RavenDB-如何将相同类型的相关文档合并到索引投影中



假设以下类:

public class Thing
{
    public string Id { get; set; }
    public string Title{ get; set; }
    public string KeyId { get; set; }
    public CultureInfo Culture { get; set; }
}

以及以下结果类别:

public class ProjectedThing
{
    public string Id { get; set; }
    public string Title{ get; set; }
    public string KeyId { get; set; }
    public CultureInfo Culture { get; set; }
    public IEnumerable<Thing> Things { get; set; }
}

如何构建包含结果类的索引?

我最接近的是以下索引定义:

public class ProjectedThings : AbstractIndexCreationTask<Thing,ProjectedThing>
{
    public ProjectedThings()
    {
        Map = docs => from doc in docs
                    select new
                    {
                        Title = doc.Title,
                        KeyId = doc.KeyId,
                        Culture = doc.Culture,
                        Things = new[] { 
                            new Thing{
                                Id = doc.Id,
                                Title = doc.Title,
                                KeyId = doc.KeyId,
                                Culture = doc.Culture,
                                TitlePluralized = doc.TitlePluralized
                            }
                        }
                    };
        Reduce = results => from r in results
                            group r by r.KeyId into g
                            select new
                            {
                                Title = g.FirstOrDefault(x => x.Id == x.KeyId).Title,
                                KeyId = g.Key,
                                Culture = g.FirstOrDefault(x => x.Id == x.KeyId).Culture,
                                Things = from thing in g.SelectMany(x => x.Things).Where(x => x.Id != x.KeyId)
                                               select new
                                               {
                                                   Id = thing.Id,
                                                   Title = thing.Title,
                                                   KeyId = thing.Key,
                                                   Culture = thing.Culture
                                               }
                            };
    }
}

这几乎达到了目的,但我无法在减少中收集标题、KeyId和文化。只有Things属性正在被填充。

查看您的代码:

g.FirstOrDefault(x => x.Id == x.KeyId)

我不明白这一点,但这可能总是错误的。你可能想要:

    g.FirstOrDefault().Title,

相关内容

  • 没有找到相关文章

最新更新