Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# linqc群的求和_C#_Linq_Linq To Sql_Linq To Entities - Fatal编程技术网

C# linqc群的求和

C# linqc群的求和,c#,linq,linq-to-sql,linq-to-entities,C#,Linq,Linq To Sql,Linq To Entities,下面的示例代码没有返回正确的总和,因为它首先计算总和,然后再计算分组依据 我如何先分组,然后计算总数 下面是转储的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 ( S

下面的示例代码没有返回正确的总和,因为它首先计算总和,然后再计算分组依据

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

下面是转储的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]

ID为3的两行看起来相同。所有ID都是这样吗?您使用的Linq查询的语义将对所有这些重复项求和,如果它们是这样的话。您是否真的希望使用distinct而不是group by?我需要在linq QUERY中的联接表之后通过分组求和total orderItem.PriceExclutax您使用的是什么?Linq到SQL还是Linq到实体?你到底想达到什么目的?不要在小组中交谈等。。。您正在尝试获取每个订单的总价吗?我正在尝试获取TotalPriceExclutax=g.Sumx=>x.orderItem.PriceExclutax,TotalPriceExclutaxStore=g.Sumx=>x.orderItem.PriceExclutax*x.p.ppStoreManager,。。。对于每个商店的OrderItems,我使用linq to entities by autofacioc=控制反转
group new { orderItem, o,p } by new { orderItem.Id } into g
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]