简体   繁体   English

MongoDB C#LINQ-子文档总和(通过AsQueryable)

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

I try use function SUM in LINQ but doesn't work well with subdocuments. 我尝试在LINQ中使用函数SUM,但不适用于子文档。 Has anyone try use SUM for subdocuments ?. 有没有人尝试将SUM用于子文档?

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. TotalAmount,始终返回值0。

Thanks, 谢谢,

Mongo c# LINQ driver don't support SUM properly. Mongo C#LINQ驱动程序不正确支持SUM。 https://jira.mongodb.org/browse/CSHARP-592 https://jira.mongodb.org/browse/CSHARP-592

Its better to use MongoQuery with aggregation sum. 最好将MongoQuery与聚合和一起使用。 Please see the link for detail usage and example. 请查看链接以获取详细用法和示例。 https://docs.mongodb.org/manual/reference/operator/aggregation/sum/ https://docs.mongodb.org/manual/reference/operator/aggregation/sum/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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