实体框架groupby然后计算各个列

时间:2018-03-13 18:45:25

标签: c# .net entity-framework c#-4.0 entity-framework-6

我有一个实体框架查询来加入具有种族表格和族群(Id和选项)的应用程序,然后得到每个种族的总数,然后根据状态{open,Closed,Draft ...获得个人计数。等等,因为我需要使用分页来获得第一个' x'或者去页面' y'所以我不能做toList然后得到值。

当前的EF查询速度很慢,需要花费更多时间。

任何人都可以帮助我以更好的方式获得相同的结果吗?

 var query = from app in Context.Application
                    join eth in Context.Ethnicities on app.EthnicityId equals eth.EthnicityId
                    group app by new
                    {
                        eth.EthnicityId,
                        eth.EthnicityOptions
                    }
                    into ethAgg
                    select new EthnicityView
                    {
                        Id = ethAgg.Key.EthnicityId,
                        Ethnicity = ethAgg.Key.EthnicityOptions,
                        Total = ethAgg.Count(),
                        Closed = ethAgg.Count(p => p.Closed),
                        Draft = ethAgg.Count(p => p.Draft ),
                        Inprogress = ethAgg.Count(p => p.Inprogress ),
                        Waiting = ethAgg.Count(p => p.Waiting),
                        Open = ethAgg.Count(p => p.Open )
                    };

生成的Sql

SELECT TOP (10) 
[Project7].[EthnicityID] AS [EthnicityID], 
[Project7].[C2] AS [C1], 
[Project7].[C1] AS [C2], 
[Project7].[C3] AS [C3], 
[Project7].[C4] AS [C4], 
[Project7].[C5] AS [C5], 
[Project7].[C6] AS [C6], 
[Project7].[C7] AS [C7]
FROM ( SELECT 
    [Project6].[C1] AS [C1], 
    [Project6].[EthnicityID] AS [EthnicityID], 
    [Project6].[EthnicityOptions] AS [C2], 
    [Project6].[C2] AS [C3], 
    [Project6].[C3] AS [C4], 
    [Project6].[C4] AS [C5], 
    [Project6].[C5] AS [C6], 
    [Project6].[C6] AS [C7]
    FROM ( SELECT 
        [Project5].[C1] AS [C1], 
        [Project5].[EthnicityID] AS [EthnicityID], 
        [Project5].[EthnicityOptions] AS [EthnicityOptions], 
        [Project5].[C2] AS [C2], 
        [Project5].[C3] AS [C3], 
        [Project5].[C4] AS [C4], 
        [Project5].[C5] AS [C5], 
        (SELECT 
            COUNT(1) AS [A1]
            FROM   [dbo].[Application] AS [Extent16]
            INNER JOIN [dbo].[Ethnicity] AS [Extent18] ON [Extent16].[EthnicityID] = [Extent18].[EthnicityID]
            WHERE ([Extent16].[Closed] = 1)
        FROM ( SELECT 
            [Project4].[C1] AS [C1], 
            [Project4].[EthnicityID] AS [EthnicityID], 
            [Project4].[EthnicityOptions] AS [EthnicityOptions], 
            [Project4].[C2] AS [C2], 
            [Project4].[C3] AS [C3], 
            [Project4].[C4] AS [C4], 
            (SELECT 
                COUNT(1) AS [A1]
                FROM   [dbo].[Application] AS [Extent13]
                INNER JOIN [dbo].[Ethnicity] AS [Extent15] ON [Extent13].[EthnicityID] = [Extent15].[EthnicityID]
                WHERE ([Extent13].[Closed] = 1)
            FROM ( SELECT 
                [Project3].[C1] AS [C1], 
                [Project3].[EthnicityID] AS [EthnicityID], 
                [Project3].[EthnicityOptions] AS [EthnicityOptions], 
                [Project3].[C2] AS [C2], 
                [Project3].[C3] AS [C3], 
                (SELECT 
                    COUNT(1) AS [A1]
                    FROM   [dbo].[Application] AS [Extent10]
                    INNER JOIN [dbo].[Ethnicity] AS [Extent12] ON [Extent10].[EthnicityID] = [Extent12].[EthnicityID]
                    WHERE ([Extent10].[Draft] = 1)
                FROM ( SELECT 
                    [Project2].[C1] AS [C1], 
                    [Project2].[EthnicityID] AS [EthnicityID], 
                    [Project2].[EthnicityOptions] AS [EthnicityOptions], 
                    [Project2].[C2] AS [C2], 
                    (SELECT 
                        COUNT(1) AS [A1]
                        FROM   [dbo].[Application] AS [Extent7]
                        INNER JOIN [dbo].[Ethnicity] AS [Extent9] ON [Extent7].[EthnicityID] = [Extent9].[EthnicityID]
                        WHERE ([Extent7].[Inprogress] = 1)
                    FROM ( SELECT 
                        [Project1].[C1] AS [C1], 
                        [Project1].[EthnicityID] AS [EthnicityID], 
                        [Project1].[EthnicityOptions] AS [EthnicityOptions], 
                        (SELECT 
                            COUNT(1) AS [A1]
                            FROM   [dbo].[Application] AS [Extent4]
                            INNER JOIN [dbo].[Ethnicity] AS [Extent6] ON [Extent4].[EthnicityID] = [Extent6].[EthnicityID]
                            WHERE ([Extent4].[Waiting] = 1)
                        FROM ( SELECT 
                            [GroupBy1].[A1] AS [C1], 
                            [GroupBy1].[K1] AS [EthnicityID], 
                            [GroupBy1].[K2] AS [EthnicityOptions]
                            FROM ( SELECT 
                                [Extent3].[EthnicityID] AS [K1], 
                                [Extent3].[EthnicityOptions] AS [K2], 
                                COUNT(1) AS [A1]
                                FROM   [dbo].[Application] AS [Extent1]
                                INNER JOIN [dbo].[Ethnicity] AS [Extent3] ON [Extent1].[EthnicityID] = [Extent3].[EthnicityID]
                                GROUP BY [Extent3].[EthnicityID], [Extent3].[EthnicityOptions]
                            )  AS [GroupBy1]
                        )  AS [Project1]
                    )  AS [Project2]
                )  AS [Project3]
            )  AS [Project4]
        )  AS [Project5]
    )  AS [Project6]
)  AS [Project7]
ORDER BY [Project7].[EthnicityID] ASC

1 个答案:

答案 0 :(得分:0)

由于我们无法从实体框架中实现这一目标。 我尝试将查询移至存储过程,并拆分单个计数,然后从实体框架调用该存储的proc。