RavenDB 查询子集合多态性

RavenDB query child collection polymorphism

我在 RavenDB 中有一个包含子集合的文档。子集合包含以下基本类型。

public class Section
{
    public string BackgroundColor { get; set; }
    public string Description { get; set; }
    public string DesktopBackgroundImageUrl { get; set; }
    public DateTime? EndDate { get; set; }
    public string MobileBackgroundImageUrl { get; set; }
    public int SortOrder { get; set; }
    public DateTime? StartDate { get; set; }
    public string TextColor { get; set; }
    public string Title { get; set; }
    public SectionType Type { get; set; }
}

从这种类型派生了一些 类,其中之一就是这个。

public class OfferSection : Section
{
    public IEnumerable<Merchant> Merchants { get; set; }
}

我遇到的问题是我需要查询这个子集合并获取包含派生类型的文档,然后查询它的值。

这是我到目前为止所要做的,但是因为它使用的是基本类型,Merchants 属性 不存在

public class Hubs_ByMerchantId : AbstractIndexCreationTask<Hub>
{
    public Hubs_ByMerchantId()
    {
        Map = hubs => from hub in hubs
            select new
            {
                Sections_Merchants_Id = hub.Sections.Where(x => x.Type == SectionType.Offer).SelectMany(x => x.Merchants.Select(y => y.Id))
            };
    }
}

谁能给我指出正确的方向?

谢谢

你可以使用.OfType<Type>得到你想要的:

hub.Sections.OfType<OfferSection>().SelectMany(x => x.Merchants.Select(y => y.Id));

在弄乱了我让它工作的方式之后,我将它投射到 select 中。这是可行的,因为我们已经针对该部分保存了一个类型枚举,因此很容易进行转换。

public class Hubs_ByMerchantId : AbstractIndexCreationTask<Hub>
{
    public Hubs_ByMerchantId()
    {
        Map = hubs => hubs.SelectMany(x => (IEnumerable<OfferSection>)x.Sections).Where(x => x.Type == SectionType.Offer).Select(x => new
        {
            Sections_Merchants_Id = x.Merchants.Select(y => y.Id)
        });
    }
}

这可能不是最好的解决方案,但它是唯一有效的方法