好的,我有这些 POCO
public class Product
{
public int Id { get; set; }
public string Name { get; set; }
public string Manufacturer { get; set; }
}
public class Offer
{
public int Id { get; set; }
public User Seller { get; set; }
public Product Product { get; set; }
public decimal Price { get; set; }
}
我有许多从 Product 派生的类,Offer 类的产品属性可以是其中任何一个。
我想定义静态索引,返回每个产品价格最低的报价,然后按产品类型对其进行过滤。
我走到这一步了。
Map = offers => offers.Select(x => new Offer { Product = x.Product, Price = x.Price });
Reduce = results => from r in results
group r by r.Product into g
select new Offer { Product = g.Key , Price = g.Min(x => x.Price) };
这确实有效,并且给了我最低的价格,尽管我认为 Map 应该只使用 Product.Id,而不是全部(我只是不知道该怎么做)。
当我查询索引时:
Session.Query<Offer>("BestOffersIndex").Where(offer => offer.Product is DerivedProduct)
它只是忽略了 Where 条件。
如果我这样做:
Map = offers => offers.Select(x => new OfferIndexResult { ProductType = MetadataFor(x.Product)["Raven-Entity-Name"].ToString(), ProductId = x.Product.Id, Price = x.Price });
Reduce = results => from r in results
group r by r.ProductId into g
select new OfferIndexResult { ProductType = g.FirstOrDefault().ProductType, ProductId = g.Key, Price = g.Min(x => x.Price) };
TransformResults = (database, offers) => from offer in offers
let product = database.Load<Product>(offer.ProductId.ToString())
select new Offer { Product = product, Price = offer.Price };
实时投影中加载的产品为空,我不知道如何使用 ProductType 进行过滤。
任何帮助表示赞赏。