RavenDB using filter with group by

故事扮演 提交于 2021-01-27 20:01:14

问题


I have a Transaction entity.

I can make group by by (CustomerCode, CustomerName) then select CustomerCode and Total(Amount).

It is easy. But When I Want to filter AtCreated. I have An Error.

Unhandled exception. Raven.Client.Exceptions.InvalidQueryException: Raven.Client.Exceptions.InvalidQueryException: Field 'AtCreated' isn't neither an aggregation operation nor part of the group by key Query: from Transactions group by CustomerCode, CustomerName where AtCreated >= $p0 select CustomerCode, count() as Total Parameters: {"p0":"2019-01-01T00:00:00.0000000"}

    public class Transactions
   {
        public string Id { get; set; }
        public long TransId { get; set; }
        public DateTime AtCreated { get; set; }
        public string CustomerCode { get; set; }
        public string CustomerName { get; set; }
        public string City { get; set; }
        public double Amount { get; set; }
        public string GXF { get; set; }

    }

var transactList = session.Query<Transactions>()
                    .Where(a=>a.AtCreated >= new DateTime(2019,01,01))
                    .GroupBy(a => new {a.CustomerCode, a.CustomerName})
                    .Select(a => new {a.Key.CustomerCode, Total = a.Count()})
                    .ToList();

How can I Grouping filtered data?

Thank You.


回答1:


Create a Map-Reduce Index and then query on it.
https://ravendb.net/docs/article-page/4.2/csharp/indexes/map-reduce-indexes

For example, in this example, you can query on 'Category' field because it was indexed (meaning it was part of the Map-Reduce index definition)

See short demo examples in: https://demo.ravendb.net/



来源:https://stackoverflow.com/questions/59947955/ravendb-using-filter-with-group-by

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!