为什么Entity Framework Lambda将2个组连接转换为Union All?

时间:2017-05-03 18:54:01

标签: c# sql entity-framework linq lambda

我有一个包含多个连接和两个组连接的查询。组连接是子记录,可以附加一个或多个记录,我想全部显示它们。 查询有效,但速度极慢。 当使用多个组连接时,实体框架将其写为联合全部。这是我不理解的。 有没有办法重写查询,以便ef不会将其写为联合所有?或者我必须找到另一种解决方案吗?

这是lambda代码

var query = db.DocumentModsums
                .Join(db.Documents, 
                    data => data.DocumentId, 
                    d => d.Id, 
                    (data, d) => new
                    {
                        DocumentModsum = data,
                        Document = d,
                    })
                .Join(db.Modsums, 
                    data => data.DocumentModsum.ModsumId,
                    m => m.Id,
                    (data, m) => new
                    {
                        DocumentModsum = data.DocumentModsum,
                        Document = data.Document,
                        Modsum = m,
                    })
                .Join(db.MethodsOwnerships, 
                    data => data.Document.MethodsOwnershipId,
                    mo => mo.Id,
                    (data, mo) => new
                    {
                        DocumentModsum = data.DocumentModsum,
                        Document = data.Document,
                        Modsum = data.Modsum,
                        MethodsOwnership = mo,
                    })
                .Join(db.DocumentStatus,
                    data => data.Document.DocumentStatusId,
                    ds => ds.Id,
                    (data, ds) => new
                    {
                        DocumentModsum = data.DocumentModsum,
                        Document = data.Document,
                        Modsum = data.Modsum,
                        MethodsOwnership = data.MethodsOwnership,
                        DocumentStatus = ds,
                    })
                .Join(db.DocumentTypes,
                    data => data.Document.DocumentTypeId,
                    dt => dt.Id,
                    (data, dt) => new
                    {
                        DocumentModsum = data.DocumentModsum,
                        Document = data.Document,
                        Modsum = data.Modsum,
                        MethodsOwnership = data.MethodsOwnership,
                        DocumentStatus = data.DocumentStatus,
                        DocumentType = dt,
                    })
                .Join(db.ModsumStatus,
                    data => data.Modsum.ModsumStatusId,
                    ms => ms.Id,
                    (data, ms) => new
                    {
                        DocumentModsum = data.DocumentModsum,
                        Document = data.Document,
                        Modsum = data.Modsum,
                        MethodsOwnership = data.MethodsOwnership,
                        DocumentStatus = data.DocumentStatus,
                        DocumentType = data.DocumentType,
                        ModsumStatus = ms,
                    })
                .Join(db.ModsumTypes, 
                    data => data.Modsum.ModsumTypeId, 
                    mt => mt.Id, 
                    (data, mt) => new
                    {
                        DocumentModsum = data.DocumentModsum,
                        Document = data.Document,
                        Modsum = data.Modsum,
                        MethodsOwnership = data.MethodsOwnership,
                        DocumentStatus = data.DocumentStatus,
                        DocumentType = data.DocumentType,
                        ModsumStatus = data.ModsumStatus,
                        ModsumType = mt,
                    })
                .GroupJoin(db.SummaryOfActions,
                    data => data.Document.Id,
                    soa => soa.DocumentId,
                    (data, soa) => new
                    {
                        DocumentModsum = data.DocumentModsum,
                        Document = data.Document,
                        Modsum = data.Modsum,
                        MethodsOwnership = data.MethodsOwnership,
                        DocumentStatus = data.DocumentStatus,
                        DocumentType = data.DocumentType,
                        ModsumStatus = data.ModsumStatus,
                        ModsumType = data.ModsumType,
                        CIN = soa.Join(db.CINs,
                                soa2 => soa2.CINId,
                                cin2 => cin2.Id,
                                (soa2, cin2) => new
                                {
                                    CIN = cin2,
                                    SOA = soa2
                                }
                                ).Where(s => s.SOA.DeletedAt == null && s.CIN.DeletedAt == null)
                                .GroupBy(c => new { c.CIN.CINName })
                                .Select(c => c.Key.CINName)
                                .ToList()

                    })                    
                .GroupJoin(
                    db.ModsumEffectivities, 
                    data => data.Modsum.Id, 
                    me => me.ModsumId, 
                    (data, me) => new
                    {
                        Data = data,
                        ModsumEffectivity = me.Where(meff => meff.DeletedAt == null).Select(m2 => new ModsumEffectivityVM()
                        {
                            AircraftFrom = m2.AircraftFromId.ToString(),
                            AircraftFromId = m2.AircraftFromId,
                            AircraftTo = m2.AircraftToId.ToString(),
                            AircraftToId = m2.AircraftToId,                                
                            AircraftModelId = m2.AircraftModelId
                        })
                        .ToList()
                    })                    
                .Where(dm => dm.Data.DocumentModsum.DeletedAt == null
                    && dm.Data.Document.DeletedAt == null
                    && dm.Data.Modsum.DeletedAt == null
                    && dm.Data.MethodsOwnership.DeletedAt == null
                    && dm.Data.DocumentStatus.DeletedAt == null
                    && dm.Data.DocumentType.DeletedAt == null
                    && dm.Data.ModsumStatus.DeletedAt == null
                    && dm.Data.ModsumType.DeletedAt == null
                )
                .Select(data => new DocumentVM()
                {
                    Id = data.Data.DocumentModsum.Id,
                    Status = data.Data.DocumentStatus.Description,
                    DocumentNumber = data.Data.Document.DocumentNumber,
                    DocumentRevision = data.Data.Document.DocumentRevision,
                    DocumentTitle = data.Data.Document.DocumentTitle,
                    ModsumNumber = data.Data.Modsum.ModsumNumber,
                    ModsumRevision = data.Data.Modsum.ModsumRevision,
                    ModsumTitle = data.Data.Modsum.ModsumTitle,
                    DocumentType = data.Data.DocumentType.Description,
                    MethodsOwnership = data.Data.MethodsOwnership.Description,
                    ModsumReleaseDate = data.Data.Modsum.MSReleaseDate,
                    ModsumAuthority = data.Data.Modsum.Authority ?? "",
                    CreatedAt = data.Data.DocumentModsum.CreatedAt,
                    CIN = data.Data.Document.CIN,
                    CINList = data.Data.CIN,
                    ModsumEffectivity = data.ModsumEffectivity,                        
                }).ToList();

转换为

SELECT 
[UnionAll1].[Id] AS [C1], 
[UnionAll1].[Id1] AS [C2], 
[UnionAll1].[Id2] AS [C3], 
[UnionAll1].[Id3] AS [C4], 
[UnionAll1].[Id4] AS [C5], 
[UnionAll1].[Id5] AS [C6], 
[UnionAll1].[Id6] AS [C7], 
[UnionAll1].[C2] AS [C8], 
[UnionAll1].[Id7] AS [C9], 
[UnionAll1].[Description] AS [C10], 
[UnionAll1].[DocumentNumber] AS [C11], 
[UnionAll1].[DocumentRevision] AS [C12], 
[UnionAll1].[DocumentTitle] AS [C13], 
[UnionAll1].[ModsumNumber] AS [C14], 
[UnionAll1].[ModsumRevision] AS [C15], 
[UnionAll1].[ModsumTitle] AS [C16], 
[UnionAll1].[Description1] AS [C17], 
[UnionAll1].[Description2] AS [C18], 
[UnionAll1].[MSReleaseDate1] AS [C19], 
[UnionAll1].[C3] AS [C20], 
[UnionAll1].[CreatedAt] AS [C21], 
[UnionAll1].[CIN] AS [C22], 
[UnionAll1].[C1] AS [C23], 
[UnionAll1].[CINName] AS [C24], 
[UnionAll1].[C4] AS [C25], 
[UnionAll1].[C5] AS [C26]
FROM  (SELECT 
    [Project3].[C1] AS [C1], 
    [Limit1].[MSReleaseDate] AS [MSReleaseDate], 
    [Limit1].[Id3] AS [Id], 
    [Limit1].[Id4] AS [Id1], 
    [Limit1].[Id5] AS [Id2], 
    [Limit1].[Id6] AS [Id3], 
    [Limit1].[Id7] AS [Id4], 
    [Limit1].[Id1] AS [Id5], 
    [Limit1].[Id2] AS [Id6], 
    [Limit1].[C1] AS [C2], 
    [Limit1].[Id] AS [Id7], 
    [Limit1].[Description1] AS [Description], 
    [Limit1].[DocumentNumber] AS [DocumentNumber], 
    [Limit1].[DocumentRevision] AS [DocumentRevision], 
    [Limit1].[DocumentTitle] AS [DocumentTitle], 
    [Limit1].[ModsumNumber] AS [ModsumNumber], 
    [Limit1].[ModsumRevision] AS [ModsumRevision], 
    [Limit1].[ModsumTitle] AS [ModsumTitle], 
    [Limit1].[Description2] AS [Description1], 
    [Limit1].[Description] AS [Description2], 
    [Limit1].[MSReleaseDate] AS [MSReleaseDate1], 
    [Limit1].[C2] AS [C3], 
    [Limit1].[CreatedAt] AS [CreatedAt], 
    [Limit1].[CIN] AS [CIN], 
    [Project3].[CINName] AS [CINName], 
    CAST(NULL AS int) AS [C4], 
    CAST(NULL AS bit) AS [C5]
    FROM   (SELECT [Project1].[Id] AS [Id], [Project1].[CreatedAt] AS [CreatedAt], [Project1].[Id1] AS [Id1], [Project1].[DocumentNumber] AS [DocumentNumber], [Project1].[DocumentRevision] AS [DocumentRevision], [Project1].[DocumentTitle] AS [DocumentTitle], [Project1].[CIN] AS [CIN], [Project1].[Id2] AS [Id2], [Project1].[ModsumNumber] AS [ModsumNumber], [Project1].[ModsumRevision] AS [ModsumRevision], [Project1].[ModsumTitle] AS [ModsumTitle], [Project1].[MSReleaseDate] AS [MSReleaseDate], [Project1].[Id3] AS [Id3], [Project1].[Description] AS [Description], [Project1].[Id4] AS [Id4], [Project1].[Description1] AS [Description1], [Project1].[Id5] AS [Id5], [Project1].[Description2] AS [Description2], [Project1].[Id6] AS [Id6], [Project1].[Id7] AS [Id7], [Project1].[C1] AS [C1], [Project1].[C2] AS [C2]
        FROM ( SELECT 
            [Filter1].[Id8] AS [Id], 
            [Filter1].[CreatedAt1] AS [CreatedAt], 
            [Filter1].[Id9] AS [Id1], 
            [Filter1].[DocumentNumber] AS [DocumentNumber], 
            [Filter1].[DocumentRevision] AS [DocumentRevision], 
            [Filter1].[DocumentTitle] AS [DocumentTitle], 
            [Filter1].[CIN] AS [CIN], 
            [Filter1].[Id10] AS [Id2], 
            [Filter1].[ModsumNumber] AS [ModsumNumber], 
            [Filter1].[ModsumRevision] AS [ModsumRevision], 
            [Filter1].[ModsumTitle] AS [ModsumTitle], 
            [Filter1].[MSReleaseDate] AS [MSReleaseDate], 
            [Filter1].[Id11] AS [Id3], 
            [Filter1].[Description3] AS [Description], 
            [Filter1].[Id12] AS [Id4], 
            [Filter1].[Description4] AS [Description1], 
            [Filter1].[Id13] AS [Id5], 
            [Filter1].[Description5] AS [Description2], 
            [Filter1].[Id14] AS [Id6], 
            [Extent8].[Id] AS [Id7], 
            1 AS [C1], 
            CASE WHEN ([Filter1].[Authority] IS NULL) THEN N'' ELSE [Filter1].[Authority] END AS [C2]
            FROM   (SELECT [Extent1].[Id] AS [Id8], [Extent1].[CreatedAt] AS [CreatedAt1], [Extent2].[Id] AS [Id9], [Extent2].[DocumentNumber] AS [DocumentNumber], [Extent2].[DocumentRevision] AS [DocumentRevision], [Extent2].[DocumentTitle] AS [DocumentTitle], [Extent2].[CIN] AS [CIN], [Extent3].[Id] AS [Id10], [Extent3].[ModsumNumber] AS [ModsumNumber], [Extent3].[ModsumRevision] AS [ModsumRevision], [Extent3].[ModsumTitle] AS [ModsumTitle], [Extent3].[MSReleaseDate] AS [MSReleaseDate], [Extent3].[ModsumTypeId] AS [ModsumTypeId], [Extent3].[Authority] AS [Authority], [Extent4].[Id] AS [Id11], [Extent4].[Description] AS [Description3], [Extent5].[Id] AS [Id12], [Extent5].[Description] AS [Description4], [Extent6].[Id] AS [Id13], [Extent6].[Description] AS [Description5], [Extent7].[Id] AS [Id14]
                FROM       [dbo].[DocumentModsums] AS [Extent1]
                INNER JOIN [dbo].[Documents] AS [Extent2] ON [Extent1].[DocumentId] = [Extent2].[Id]
                INNER JOIN [dbo].[Modsums] AS [Extent3] ON [Extent1].[ModsumId] = [Extent3].[Id]
                INNER JOIN [dbo].[MethodsOwnerships] AS [Extent4] ON [Extent2].[MethodsOwnershipId] = [Extent4].[Id]
                INNER JOIN [dbo].[DocumentStatus] AS [Extent5] ON [Extent2].[DocumentStatusId] = [Extent5].[Id]
                INNER JOIN [dbo].[DocumentTypes] AS [Extent6] ON [Extent2].[DocumentTypeId] = [Extent6].[Id]
                INNER JOIN [dbo].[ModsumStatus] AS [Extent7] ON [Extent3].[ModsumStatusId] = [Extent7].[Id]
                WHERE ([Extent1].[DeletedAt] IS NULL) AND ([Extent2].[DeletedAt] IS NULL) AND ([Extent3].[DeletedAt] IS NULL) AND ([Extent4].[DeletedAt] IS NULL) AND ([Extent5].[DeletedAt] IS NULL) AND ([Extent6].[DeletedAt] IS NULL) AND ([Extent7].[DeletedAt] IS NULL) ) AS [Filter1]
            INNER JOIN [dbo].[ModsumTypes] AS [Extent8] ON [Filter1].[ModsumTypeId] = [Extent8].[Id]
            WHERE [Extent8].[DeletedAt] IS NULL
        )  AS [Project1]
        ORDER BY [Project1].[MSReleaseDate] DESC
        OFFSET 0 ROWS FETCH NEXT 25 ROWS ONLY  ) AS [Limit1]
    OUTER APPLY  (SELECT 
        [Distinct1].[CINName] AS [CINName], 
        1 AS [C1]
        FROM ( SELECT DISTINCT 
            [Extent10].[CINName] AS [CINName]
            FROM  [dbo].[SummaryOfActions] AS [Extent9]
            INNER JOIN [dbo].[CINs] AS [Extent10] ON [Extent9].[CINId] = [Extent10].[Id]
            WHERE ([Limit1].[Id1] = [Extent9].[DocumentId]) AND ([Extent9].[DeletedAt] IS NULL) AND ([Extent10].[DeletedAt] IS NULL)
        )  AS [Distinct1] ) AS [Project3]
UNION ALL
    SELECT 
    2 AS [C1], 
    [Limit2].[MSReleaseDate] AS [MSReleaseDate], 
    [Limit2].[Id3] AS [Id], 
    [Limit2].[Id4] AS [Id1], 
    [Limit2].[Id5] AS [Id2], 
    [Limit2].[Id6] AS [Id3], 
    [Limit2].[Id7] AS [Id4], 
    [Limit2].[Id1] AS [Id5], 
    [Limit2].[Id2] AS [Id6], 
    [Limit2].[Id] AS [Id7], 
    [Limit2].[Id] AS [Id8], 
    [Limit2].[Description1] AS [Description], 
    [Limit2].[DocumentNumber] AS [DocumentNumber], 
    [Limit2].[DocumentRevision] AS [DocumentRevision], 
    [Limit2].[DocumentTitle] AS [DocumentTitle], 
    [Limit2].[ModsumNumber] AS [ModsumNumber], 
    [Limit2].[ModsumRevision] AS [ModsumRevision], 
    [Limit2].[ModsumTitle] AS [ModsumTitle], 
    [Limit2].[Description2] AS [Description1], 
    [Limit2].[Description] AS [Description2], 
    [Limit2].[MSReleaseDate] AS [MSReleaseDate1], 
    [Limit2].[C1] AS [C2], 
    [Limit2].[CreatedAt] AS [CreatedAt], 
    [Limit2].[CIN] AS [CIN], 
    CAST(NULL AS varchar(1)) AS [C3], 
    [Extent19].[ModsumId] AS [ModsumId], 
    cast(1 as bit) AS [C4]
    FROM   (SELECT [Project5].[Id] AS [Id], [Project5].[CreatedAt] AS [CreatedAt], [Project5].[Id1] AS [Id1], [Project5].[DocumentNumber] AS [DocumentNumber], [Project5].[DocumentRevision] AS [DocumentRevision], [Project5].[DocumentTitle] AS [DocumentTitle], [Project5].[CIN] AS [CIN], [Project5].[Id2] AS [Id2], [Project5].[ModsumNumber] AS [ModsumNumber], [Project5].[ModsumRevision] AS [ModsumRevision], [Project5].[ModsumTitle] AS [ModsumTitle], [Project5].[MSReleaseDate] AS [MSReleaseDate], [Project5].[Id3] AS [Id3], [Project5].[Description] AS [Description], [Project5].[Id4] AS [Id4], [Project5].[Description1] AS [Description1], [Project5].[Id5] AS [Id5], [Project5].[Description2] AS [Description2], [Project5].[Id6] AS [Id6], [Project5].[Id7] AS [Id7], [Project5].[C1] AS [C1]
        FROM ( SELECT 
            [Filter4].[Id15] AS [Id], 
            [Filter4].[CreatedAt2] AS [CreatedAt], 
            [Filter4].[Id16] AS [Id1], 
            [Filter4].[DocumentNumber] AS [DocumentNumber], 
            [Filter4].[DocumentRevision] AS [DocumentRevision], 
            [Filter4].[DocumentTitle] AS [DocumentTitle], 
            [Filter4].[CIN] AS [CIN], 
            [Filter4].[Id17] AS [Id2], 
            [Filter4].[ModsumNumber] AS [ModsumNumber], 
            [Filter4].[ModsumRevision] AS [ModsumRevision], 
            [Filter4].[ModsumTitle] AS [ModsumTitle], 
            [Filter4].[MSReleaseDate] AS [MSReleaseDate], 
            [Filter4].[Id18] AS [Id3], 
            [Filter4].[Description6] AS [Description], 
            [Filter4].[Id19] AS [Id4], 
            [Filter4].[Description7] AS [Description1], 
            [Filter4].[Id20] AS [Id5], 
            [Filter4].[Description8] AS [Description2], 
            [Filter4].[Id21] AS [Id6], 
            [Extent18].[Id] AS [Id7], 
            CASE WHEN ([Filter4].[Authority] IS NULL) THEN N'' ELSE [Filter4].[Authority] END AS [C1]
            FROM   (SELECT [Extent11].[Id] AS [Id15], [Extent11].[CreatedAt] AS [CreatedAt2], [Extent12].[Id] AS [Id16], [Extent12].[DocumentNumber] AS [DocumentNumber], [Extent12].[DocumentRevision] AS [DocumentRevision], [Extent12].[DocumentTitle] AS [DocumentTitle], [Extent12].[CIN] AS [CIN], [Extent13].[Id] AS [Id17], [Extent13].[ModsumNumber] AS [ModsumNumber], [Extent13].[ModsumRevision] AS [ModsumRevision], [Extent13].[ModsumTitle] AS [ModsumTitle], [Extent13].[MSReleaseDate] AS [MSReleaseDate], [Extent13].[ModsumTypeId] AS [ModsumTypeId], [Extent13].[Authority] AS [Authority], [Extent14].[Id] AS [Id18], [Extent14].[Description] AS [Description6], [Extent15].[Id] AS [Id19], [Extent15].[Description] AS [Description7], [Extent16].[Id] AS [Id20], [Extent16].[Description] AS [Description8], [Extent17].[Id] AS [Id21]
                FROM       [dbo].[DocumentModsums] AS [Extent11]
                INNER JOIN [dbo].[Documents] AS [Extent12] ON [Extent11].[DocumentId] = [Extent12].[Id]
                INNER JOIN [dbo].[Modsums] AS [Extent13] ON [Extent11].[ModsumId] = [Extent13].[Id]
                INNER JOIN [dbo].[MethodsOwnerships] AS [Extent14] ON [Extent12].[MethodsOwnershipId] = [Extent14].[Id]
                INNER JOIN [dbo].[DocumentStatus] AS [Extent15] ON [Extent12].[DocumentStatusId] = [Extent15].[Id]
                INNER JOIN [dbo].[DocumentTypes] AS [Extent16] ON [Extent12].[DocumentTypeId] = [Extent16].[Id]
                INNER JOIN [dbo].[ModsumStatus] AS [Extent17] ON [Extent13].[ModsumStatusId] = [Extent17].[Id]
                WHERE ([Extent11].[DeletedAt] IS NULL) AND ([Extent12].[DeletedAt] IS NULL) AND ([Extent13].[DeletedAt] IS NULL) AND ([Extent14].[DeletedAt] IS NULL) AND ([Extent15].[DeletedAt] IS NULL) AND ([Extent16].[DeletedAt] IS NULL) AND ([Extent17].[DeletedAt] IS NULL) ) AS [Filter4]
            INNER JOIN [dbo].[ModsumTypes] AS [Extent18] ON [Filter4].[ModsumTypeId] = [Extent18].[Id]
            WHERE [Extent18].[DeletedAt] IS NULL
        )  AS [Project5]
        ORDER BY [Project5].[MSReleaseDate] DESC
        OFFSET 0 ROWS FETCH NEXT 25 ROWS ONLY  ) AS [Limit2]
    INNER JOIN [dbo].[ModsumEffectivities] AS [Extent19] ON [Limit2].[Id2] = [Extent19].[ModsumId]) AS [UnionAll1]
ORDER BY [UnionAll1].[MSReleaseDate] DESC, [UnionAll1].[Id] ASC, [UnionAll1].[Id1] ASC, [UnionAll1].[Id2] ASC, [UnionAll1].[Id3] ASC, [UnionAll1].[Id4] ASC, [UnionAll1].[Id5] ASC, [UnionAll1].[Id6] ASC, [UnionAll1].[Id7] ASC, [UnionAll1].[C1] ASC

1 个答案:

答案 0 :(得分:0)

EF,或大多数ORM,说实话,他们通常会使用" safe"生成查询的方法。这意味着可能有更有效的方法来编写相同的查询,但它不会始终正常工作。这通常是生成代码的问题,当存在涵盖更多场景的解决方案时,它将在所有情况下应用。但是,正如您所指出的,可能有更好的方法来编写lambda表达式并实现更好的查询。我看到了一些我可能会尝试的事情,但是我无法在没有首先创建EF代码库来复制您的设计的情况下确保这样做。

用语言来说,我会尝试将组连接移动到它所加入的表中。所以SummaryOfActions'群组加入将存在于“文件”中。加入和ModsumEffectivities'群组加入将存在于Modsums'加入。

这是我能想到的唯一可能导致查询以更有效的方式生成的内容。

在旁注中,当您创建匿名类型(新{foo})时,您不需要指定名称(新{foo = foo}),除非您希望它们使用与变量

我尝试在记事本中对您的查询进行这些更改,因此可能会出现一些错误,但请尝试一下......

  var query = db.DocumentModsums
    .Join(db.Documents,
      data => data.DocumentId,
      d => d.Id,
      (data, doc) => new
      {
        DocumentModsum = data,
        Document = new {
          doc.MethodsOwnershipId,
          doc.DocumentStatusId,
          doc.DocumentTypeId,
          doc.DeletedAt,
          doc.DocumentNumber,
          doc.DocumentRevision,
          doc.DocumentTitle,
          doc.CIN,
          SoaCinNames = db.SummaryOfActions
                          .Join(db.CINs, soa => soa.CINId, cin => cin.Id, (soa, cin) => new { CIN = cin, SOA = soa })
                          .Where(s => s.SOA.DocumentId == doc.Id && s.SOA.DeletedAt == null && s.CIN.DeletedAt == null)
                          .GroupBy(c => c.CIN.CINName)
                          .Select(c => c.Key )}
      }
    )
    .Join(db.Modsums,
      data => data.DocumentModsum.ModsumId,
      m => m.Id,
      (data, mod) => new
      {
        data.DocumentModsum,
        data.Document,
        Modsum = new
        {
          mod.ModsumStatusId,
          mod.ModsumTypeId,
          mod.ModsumNumber,
          mod.ModsumRevision,
          mod.ModsumTitle,
          mod.MSReleaseDate,
          mod.Authority,
          mod.DeletedAt,
          Effectivities = db.ModsumEffectivities
                                         .Where(me => me.ModsumId == mod.Id && me.DeletedAt == null)
                                         .Select(m2 => new ModsumEffectivityVM
                                         {
                                           AircraftFrom = m2.AircraftFromId.ToString(),
                                           AircraftFromId = m2.AircraftFromId,
                                           AircraftTo = m2.AircraftToId.ToString(),
                                           AircraftToId = m2.AircraftToId,
                                           AircraftModelId = m2.AircraftModelId
                                         })
        }
      }
    )
    .Join(db.MethodsOwnerships,
      data => data.Document.MethodsOwnershipId,
      mo => mo.Id,
      (data, mo) => new
      {
        data.DocumentModsum,
        data.Document,
        data.Modsum,
        MethodsOwnership = mo
      })
    .Join(db.DocumentStatus,
      data => data.Document.DocumentStatusId,
      ds => ds.Id,
      (data, ds) => new
      {
        data.DocumentModsum,
        data.Document,
        data.Modsum,
        data.MethodsOwnership,
        DocumentStatus = ds
      })
    .Join(db.DocumentTypes,
      data => data.Document.DocumentTypeId,
      dt => dt.Id,
      (data, dt) => new
      {
        data.DocumentModsum,
        data.Document,
        data.Modsum,
        data.MethodsOwnership,
        data.DocumentStatus,
        DocumentType = dt
      })
    .Join(db.ModsumStatus,
      data => data.Modsum.ModsumStatusId,
      ms => ms.Id,
      (data, ms) => new
      {
        data.DocumentModsum,
        data.Document,
        data.Modsum,
        data.MethodsOwnership,
        data.DocumentStatus,
        data.DocumentType,
        ModsumStatus = ms
      })
    .Join(db.ModsumTypes,
      data => data.Modsum.ModsumTypeId,
      mt => mt.Id,
      (data, ModsumType) => new
      {
        data.DocumentModsum,
        data.Document,
        data.Modsum,
        data.MethodsOwnership,
        data.DocumentStatus,
        data.DocumentType,
        data.ModsumStatus,
        ModsumType,
      })
    .Where(data => data.DocumentModsum.DeletedAt == null
                   && data.Document.DeletedAt == null
                   && data.Modsum.DeletedAt == null
                   && data.MethodsOwnership.DeletedAt == null
                   && data.DocumentStatus.DeletedAt == null
                   && data.DocumentType.DeletedAt == null
                   && data.ModsumStatus.DeletedAt == null
                   && data.ModsumType.DeletedAt == null
    )
    .Select(data => new DocumentVM()
    {
      Id = data.DocumentModsum.Id,
      Status = data.DocumentStatus.Description,
      DocumentNumber = data.Document.DocumentNumber,
      DocumentRevision = data.Document.DocumentRevision,
      DocumentTitle = data.Document.DocumentTitle,
      ModsumNumber = data.Modsum.ModsumNumber,
      ModsumRevision = data.Modsum.ModsumRevision,
      ModsumTitle = data.Modsum.ModsumTitle,
      DocumentType = data.DocumentType.Description,
      MethodsOwnership = data.MethodsOwnership.Description,
      ModsumReleaseDate = data.Modsum.MSReleaseDate,
      ModsumAuthority = data.Modsum.Authority ?? "",
      CreatedAt = data.DocumentModsum.CreatedAt,
      CIN = data.Document.CIN,
      CINList = data.Document.SoaCinNames.ToList(),
      ModsumEffectivity = data.Modsum.Effectivities.ToList()
    }).ToList();