EF LINQ翻译:复杂查询

本文关键字:复杂 查询 翻译 LINQ EF | 更新日期: 2023-09-27 18:24:14

我有这样的代码(注意l.FirstOrDefault().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, 
                               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]

如果我添加房间,我也会得到简单的查询。分组字段的名称并以此方式使用

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().Name修复问题?谢谢

EF LINQ翻译:复杂查询

由于您没有对组进行排序,我认为您只需要组中的任何名称。您可以使用类似Max的聚合,而不是(是的,它甚至适用于字符串)。我相信这将产生一个更合理的查询。

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