EF LINQ翻译:复杂查询

时间:2015-11-24 20:18:50

标签: c# entity-framework linq group-by

我有这段代码(注意 l.FirstOrDefault()。姓名):

    var qry = from peron in db.Persons
    join room in db.Rooms on peron.Room.Id equals room.Id
    join passport in db.Passports on peron.Passport.Id equals passport.Id
    select new {peron.Fio, room.Name, passport.Number, Count = 0};
    qry = qry.GroupBy(l => new {l.Fio, l.Number})
          .Select(l => new {
                               l.Key.Fio, 
                               l.FirstOrDefault().Name, 
                               l.Key.Number, 
                               Count = l.Count()
                            });

这转化为:

SELECT 
        1 AS [C1], 
        [Project4].[Fio] AS [Fio], 
        [Project4].[C1] AS [C2], 
        [Project4].[Number] AS [Number], 
        [Project4].[C2] AS [C3]
        FROM ( SELECT 
            [Project3].[Fio] AS [Fio], 
            [Project3].[Number] AS [Number], 
            [Project3].[C1] AS [C1], 
            (SELECT 
                COUNT(1) AS [A1]
                FROM  [dbo].[People] AS [Extent6]
                INNER JOIN [dbo].[Passports] AS [Extent7] ON [Extent6].[Passport_Id] = [Extent7].[Id]
                WHERE ([Extent6].[Room_Id] IS NOT NULL) AND (([Project3].[Fio] = [Extent6].[Fio]) OR (([Project3].[Fio] IS NULL) AND ([Extent6].[Fio] IS NULL))) AND (([Project3].[Number] = [Extent7].[Number]) OR (([Project3].[Number] IS NULL) AND ([Extent7].[Number] IS NULL)))) AS [C2]
            FROM ( SELECT 
                [Distinct1].[Fio] AS [Fio], 
                [Distinct1].[Number] AS [Number], 
                (SELECT TOP (1) 
                    [Extent4].[Name] AS [Name]
                    FROM   [dbo].[People] AS [Extent3]
                    INNER JOIN [dbo].[Rooms] AS [Extent4] ON [Extent3].[Room_Id] = [Extent4].[Id]
                    INNER JOIN [dbo].[Passports] AS [Extent5] ON [Extent3].[Passport_Id] = [Extent5].[Id]
                    WHERE (([Distinct1].[Fio] = [Extent3].[Fio]) OR (([Distinct1].[Fio] IS NULL) AND ([Extent3].[Fio] IS NULL))) AND (([Distinct1].[Number] = [Extent5].[Number]) OR (([Distinct1].[Number] IS NULL) AND ([Extent5].[Number] IS NULL)))) AS [C1]
                FROM ( SELECT DISTINCT 
                    [Extent1].[Fio] AS [Fio], 
                    [Extent2].[Number] AS [Number]
                    FROM  [dbo].[People] AS [Extent1]
                    INNER JOIN [dbo].[Passports] AS [Extent2] ON [Extent1].[Passport_Id] = [Extent2].[Id]
                    WHERE [Extent1].[Room_Id] IS NOT NULL
                )  AS [Distinct1]
            )  AS [Project3]
        )  AS [Project4]

这是非常复杂的查询...但是,如果我将l.FirstOrDefault()。Name替换为Name =“”,我会得到简单的查询

var qry = from peron in db.Persons
    join room in db.Rooms on peron.Room.Id equals room.Id
    join passport in db.Passports on peron.Passport.Id equals passport.Id
    select new {peron.Fio, room.Name, passport.Number, Count = 0};
qry = qry.GroupBy(l => new {l.Fio, l.Number}).Select(l => new {l.Key.Fio, Name = "", l.Key.Number, Count = l.Count()});    

SELECT 
        1 AS [C1], 
        [GroupBy1].[K1] AS [Fio], 
        N'' AS [C2], 
        [GroupBy1].[K2] AS [Number], 
        [GroupBy1].[A1] AS [C3]
        FROM ( SELECT 
            [Extent1].[Fio] AS [K1], 
            [Extent2].[Number] AS [K2], 
            COUNT(1) AS [A1]
            FROM  [dbo].[People] AS [Extent1]
            INNER JOIN [dbo].[Passports] AS [Extent2] ON [Extent1].[Passport_Id] = [Extent2].[Id]
            WHERE [Extent1].[Room_Id] IS NOT NULL
            GROUP BY [Extent1].[Fio], [Extent2].[Number]
        )  AS [GroupBy1]

如果我将room.Name添加到分组字段并按这种方式使用

,我也会得到简单的查询
var qry = from peron in db.Persons
                    join room in db.Rooms on peron.Room.Id equals room.Id
                    join passport in db.Passports on peron.Passport.Id equals passport.Id
                    select new {peron.Fio, room.Name, passport.Number, Count = 0};
                qry = qry.GroupBy(l => new {l.Fio, l.Name, l.Number})
                      .Select(l => new 
                      {
                          l.Key.Fio, 
                          l.Key.Name, 
                          l.Key.Number, 
                          Count = l.Count()
                      });

如何解决 l.FirstOrDefault()。名称的问题? 感谢。

1 个答案:

答案 0 :(得分:1)

由于您没有订购该组,我假设您只需要该组中的任何名称。您可以使用类似Max的聚合(是的,它甚至可以在字符串上使用)。我相信这会产生更合理的查询。

new {
       l.Key.Fio, 
       Name = l.Max(x => x.Name), 
       l.Key.Number, 
       Count = l.Count()
    });