Ravendb index, a request for collecting children

I want to get a summary of all products, since only the last OrderHistory is interesting, where I want to use it. I have thousands of products with hundreds of OrderHistory each, but now I only want the product identifier and the latest OrderHistory for each product.

public class ProductSummary
{
    public int ProductId { get; set; }
    public OrderHistory LastOrderHistory { get; set; }
}

OrderHistory is stored inside the Product document as follows:

public class Product
{
    public int Id { get; set; }
    public int MarketGroupId { get; set; }
    public string Name { get; set; }
    public decimal Price { get; set; }
    public List<OrderHistory> OrderHistory { get; set; }
}

And here is what OrderHistory looks like:

public class OrderHistory
    {
        public long OrderCount { get; set; }
        public long Volume { get; set; }
        public DateTime date { get; set; }
        public double AvgPrice { get; set; }
    }

Now I have tried several approaches to the index and query to run it, this is my last attempt, but it does not return any results.

public class LatestProductOrderHistory : AbstractIndexCreationTask<Product, ProductSummary>
{
    public LatestProductOrderHistory()
    {
        Map = products => from p in products
                          from oh in p.OrderHistory
            select new
            {
                ProductId = p.Id,
                LastOrderHIstory = p.OrderHistory.OrderByDescending(o => o.date).Last()
            };

        StoreAllFields(FieldStorage.Yes);
    }
}

And finally, my request:

                var results = session
                    .Query<ProductSummary, LatestProductOrderHistory>()
                    .ProjectFromIndexFieldsInto<ProductSummary>()
                    .Take(1024)
                    .Skip(start)
                    .ToList();

This combination does not give me any results, I have never done indexes in ravendb before, so sorry if this is a stupid question.

EDIT: , , , " : Id" EDIT2: vs ravendb,

+4
1

, "" :

Reduce = results => from result in results
    group result by result.Id into g
    select new
    {
        Id = g.Key,
        LastOrderHistory = g.SelectMany(x=> x.LastOrderHistory)
            .OrderByDescending(o => o.Date).FirstOrDefault()
        }; 

"" Map/Reduce.

:

session.Query<ProductSummary, LatestProductOrderHistory>()
0

Source: https://habr.com/ru/post/1628209/


All Articles