Linq 计算特定客户的产品

Linq count products for a specific customer

您好,我有一个计算所有已售出产品的查询,现在我想更改它以计算特定客户的产品数量。任何人都可以帮助我吗?表= table 客户one2many transactions one2many transactionitems many2one product many2one producttype

            var query = from product in cse.tblTransactionItems
                        group product by product.tblProduct.Description into g
                        select new { ProductId = g.Key, totalUnitsSold = g.Count() }; 

您可以考虑按多列分组。

var query = from product in cse.tblTransactionItems
            group product by new
            {
                product.tblProduct.Description,
                product.tblProduct.Customer // put the customer here.
            } into g
            select new
            {
                Product = g.Key.Description,
                Customer = g.Key.Customer,
                totalUnitsSold = g.Count()
            }; 

这样,totalUnitsSold 就是特定客户和产品的总数。

注意:如果您的客户在另一个 table 中,您需要先加入。以上只是假设客户是您现有 table.

的一部分