Linq C#Group by Sum

时间:2015-01-10 14:09:34

标签: c# linq linq-to-sql linq-to-entities

我有以下示例代码,它不会返回正确的总和,因为它首先计算总和,然后它按组进行!

我如何先分组,然后计算总和

var result = (from orderItem in _orderItemRepository.Table
            join o in _orderRepository.Table on orderItem.OrderId equals o.Id
            join p in _productRepository.Table on orderItem.ProductId equals p.Id
            join psch in _productScheduleRepository.Table on p.Id equals psch.ProductId
            join sm in _storeMappingRepository.Table on new { c1 = p.Id, c2 = "Product" } equals new { c1 = sm.EntityId, c2 = sm.EntityName } into p_sm
            from sm in p_sm.DefaultIfEmpty()
            where ...                        

            group new { orderItem, o,p } by new { orderItem.Id } into g


              select new
              {
                  TotalPriceExclTax = g.Sum(x => x.orderItem.PriceExclTax),
                  TotalPriceExclTaxStore = g.Sum(x => x.orderItem.PriceExclTax * x.p.PPStoreManagers),
                  TotalPriceExclTaxAdmin = g.Sum(x => x.orderItem.PriceExclTax * x.p.PPAdministrators),
                  TotalPriceInclTax = g.Sum(x => x.orderItem.PriceInclTax) 
              }).FirstOrDefault();

group new { orderItem, o,p } by new { orderItem.Id } into g

example

以下是转储的linq查询以获得更好的解释:

SELECT TOP (1) 
    [GroupBy2].[A1] AS [C1], 
    [GroupBy2].[A2] AS [C2], 
    [GroupBy2].[A3] AS [C3], 
    [GroupBy2].[A4] AS [C4], 
    [GroupBy2].[K1] AS [Id]
     FROM ( SELECT 
        [Filter4].[K1] AS [K1], 
        SUM([Filter4].[A1]) AS [A1], 
        SUM([Filter4].[A2]) AS [A2], 
        SUM([Filter4].[A3]) AS [A3], 
        SUM([Filter4].[A4]) AS [A4]
        FROM ( SELECT 
            [Filter1].[Id1] AS [K1], 
            [Filter1].[PriceExclTax] AS [A1], 
            [Filter1].[PriceExclTax] * [Filter1].[PPStoreManagers] AS [A2], 
            [Filter1].[PriceExclTax] * [Filter1].[PPAdministrators] AS [A3], 
            [Filter1].[PriceInclTax] AS [A4]
            FROM   (SELECT ...
                FROM     [dbo].[OrderItem] AS [Extent1]
                INNER JOIN [dbo].[Order] AS [Extent2] ON [Extent1].[OrderId] = [Extent2].[Id]
                INNER JOIN [dbo].[Product] AS [Extent3] ON [Extent1].[ProductId] = [Extent3].[Id]
                INNER JOIN [dbo].[Product_Schedule_Mapping] AS [Extent4] ON [Extent3].[Id] = [Extent4].[ProductId]
                LEFT OUTER JOIN [dbo].[StoreMapping] AS [Extent5] ON ([Extent3].[Id] = [Extent5].[EntityId]) AND (N''Product'' = [Extent5].[EntityName])
                WHERE [Extent2].[Deleted] <> cast(1 as bit) ) AS [Filter1]
            CROSS JOIN  (SELECT [GroupBy1].[A1] AS [A1]
                FROM ( SELECT 
                    COUNT(1) AS [A1]
                    FROM  ( SELECT 1 AS X ) AS [SingleRowTable1]
                    WHERE 1 = 0
                )  AS [GroupBy1]
                WHERE 0 = [GroupBy1].[A1] ) AS [Filter3]
            WHERE (...)
        )  AS [Filter4]
        GROUP BY [K1]
    )  AS [GroupBy2]

0 个答案:

没有答案