简体   繁体   中英

MongoDB C# LINQ - Sum to subdocument (by AsQueryable)

I try use function SUM in LINQ but doesn't work well with subdocuments. Has anyone try use SUM for subdocuments ?.

My sample query:

var collection = database.GetCollection<Order>("Order").AsQueryable();
var query = from o in collection
            from p in o.OrderItems
            select p;

var query2 = from orderItem in query
             group orderItem by orderItem.ProductId into g
             select new 
             {
                  ProductId = g.Key,
                  TotalAmount = g.Sum(x=>x.UnitPriceExclTax)
             };

TotalAmount, always returned value 0.

Thanks,

Mongo c# LINQ driver don't support SUM properly. https://jira.mongodb.org/browse/CSHARP-592

Its better to use MongoQuery with aggregation sum. Please see the link for detail usage and example. https://docs.mongodb.org/manual/reference/operator/aggregation/sum/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM