我正在尝试创建一个具有与下图类似的类和索引定义的索引:
public class Foo
{
public string Tag { get; set; }
public List<Bar> Bars { get; set; }
}
public abstract class Bar
{
public int Weight { get; set; }
}
public class IronBar : Bar { }
public class ChocolateBar : Bar { }
public class TagSummary
{
public string Tag { get; set; }
public int Count { get; set; }
public int TotalChocolateBarWeight { get; set; }
public int TotalIronBarWeight { get; set; }
}
public class TagSummaryIndex : AbstractIndexCreationTask<Foo, TagSummary>
{
public TagSummaryIndex()
{
Map = foos => from f in foos
select new
{
Tag = f.Tag,
Count = 1,
TotalChocolateBarWeight = f.Bars.OfType<ChocolateBar>().Sum(x=> x.Weight),
TotalIronBarWeight = f.Bars.OfType<IronBar>().Sum(x=> x.Weight)
};
Reduce = results => from r in results
group r by r.Tag into g
select new
{
Tag = g.Key,
Count = g.Sum(x => x.Count),
TotalChocolateBarWeight = g.Sum(x => x.TotalChocolateBarWeight),
TotalIronBarWeight = g.Sum(x => x.TotalIronBarWeight)
};
}
}
但是,当我尝试创建索引时
IndexCreation.CreateIndexes(this.GetType().Assembly, _documentStore);
它抛出一个 InvalidOperationException。如果我从中删除.OfType<T>()
零件,Map
那么一切都很好(但不是我想要的)。我尝试过使用Where(x => x is ChocolateBar)
和其他各种类型检查选项,但无济于事。
我将如何实现这一目标?
谢谢