1
我想創建一個類似的類和索引定義到該索引如下圖所示:在RavenDB指數從集合檢索特定類型的項目的總和
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。如果我從Map
中刪除.OfType<T>()
部件,那麼一切都很好(但不是我想要的)。我試過使用Where(x => x is ChocolateBar)
和各種其他類型的檢查選項,但無濟於事。
我該如何去實現這一目標?
感謝
感謝馬特,是的,這對我來說是一個方便的功能。我會暫時預先計算它們。 – 2013-04-06 08:12:35