使用联接和按列别名的分页顺序时,EF Core 2中的Linq查询不起作用

时间:2018-02-01 10:32:44

标签: c# sql sql-server entity-framework linq

我们目前正在尝试使用LinqEF Core 2将排序写入我们的服务器端分页。我们遇到的问题是Linq生成的列别名在使用分页时不起作用。但是,如果我们不对其进行分页,则可以按预期工作。

输出查询中的所有列都是别名,因为我们在模型中有不同的属性名称,并且数据库列名称不同,但这不会对我们的知识产生影响。

这是没有分页的Linq查询:

var source = from p in _ppmRepository.GetAll()
                         join jt in _jobTypeRepository.GetAll() on p.PpmFkeyInSeq equals jt.Id into jtdata
                         from jt in jtdata.DefaultIfEmpty()
                         join a in _assetRepository.GetAll() on p.PpmFkeyArSeq equals a.Id into aData
                         from a in aData.DefaultIfEmpty()
                         where p.PpmFkeyBgSeq == bldId
                         orderby p.PpmFreq
                         select new BuildingPpmListViewModel
                         {
                             PpmId = p.Id,
                             PpmFreq = p.PpmFreq,
                             PpmNextService = p.PpmNextService,
                             TotalCost = p.TotalCost,
                             PpmPeriodUnits = p.PpmPeriodUnits,
                             PpmFkeyPriDesc = p.PpmFkeyPriDesc,
                             JtTitle = jt.JtTitle,
                             AssetId = p.PpmFkeyArSeq,
                             AssetDescription = a.AssetDescription,
                             IsDeleted = p.IsDeleted

                         };
            source = source.Where(i => i.JtTitle.Contains("audit") && i.AssetDescription.Contains("df"));

这是由ef core生成的输出查询:

SELECT [p].[PPM_SEQ] AS [PpmId], [p].[PPM_FREQ] AS [PpmFreq], [p].[PPM_NEXT_SERVICE] AS [PpmNextService], 
CAST([p].[TotalCost] AS float) AS [TotalCost], [p].[PPM_PERIOD_UNITS] AS [PpmPeriodUnits], [p].[PPM_FKEY_PRI_DESC] AS [PpmFkeyPriDesc], 
[t].[jt_title] AS [JtTitle], [p].[PPM_FKEY_AR_SEQ] AS [AssetId], [t0].[AR_DESCRIPTION] AS [AssetDescription], [p].[Deleted] AS [IsDeleted]
FROM [PPMs] AS [p]
LEFT JOIN (
    SELECT [j].*
    FROM [JobTypes] AS [j]
) AS [t] ON [p].[PPM_FKEY_IN_SEQ] = [t].[jt_seq]
LEFT JOIN (
    SELECT [a].*
    FROM [Assets] AS [a]
) AS [t0] ON [p].[PPM_FKEY_AR_SEQ] = [t0].[ar_seq]
WHERE ([p].[PPM_FKEY_BG_SEQ] = 172) AND ((CHARINDEX(N'audit', [t].[jt_title]) > 0) AND (CHARINDEX(N'df', [t0].[AR_DESCRIPTION]) > 0))
ORDER BY [PpmFreq]

这是带有分页的Linq查询:

var source = from p in _ppmRepository.GetAll()

                         join jt in _jobTypeRepository.GetAll() on p.PpmFkeyInSeq equals jt.Id into jtdata
                         from jt in jtdata.DefaultIfEmpty()
                         join a in _assetRepository.GetAll() on p.PpmFkeyArSeq equals a.Id into aData
                         from a in aData.DefaultIfEmpty()
                         where p.PpmFkeyBgSeq == bldId
                         orderby p.PpmFreq
                         select new BuildingPpmListViewModel
                         {
                             PpmId = p.Id,
                             PpmFreq = p.PpmFreq,
                             PpmNextService = p.PpmNextService,
                             TotalCost = p.TotalCost,
                             PpmPeriodUnits = p.PpmPeriodUnits,
                             PpmFkeyPriDesc = p.PpmFkeyPriDesc,
                             JtTitle = jt.JtTitle,
                             AssetId = p.PpmFkeyArSeq,
                             AssetDescription = a.AssetDescription,
                             IsDeleted = p.IsDeleted

                         };
            source = source.Where(i => i.JtTitle.Contains("audit") && i.AssetDescription.Contains("df")).Skip(0).Take(50);

这是分页的输出,其中PpmFreq的over函数顺序是SQL无法找到的[p].[PPM_FREQ]的别名:

SELECT [t1].[PpmId], [t1].[PpmFreq], [t1].[PpmNextService], [t1].[TotalCost], [t1].[PpmPeriodUnits], 
[t1].[PpmFkeyPriDesc], [t1].[JtTitle], [t1].[AssetId], [t1].[AssetDescription], [t1].[IsDeleted]
FROM (
    SELECT [p].[PPM_SEQ] AS [PpmId], [p].[PPM_FREQ] AS [PpmFreq], [p].[PPM_NEXT_SERVICE] AS [PpmNextService], 
    CAST([p].[TotalCost] AS float) AS [TotalCost], [p].[PPM_PERIOD_UNITS] AS [PpmPeriodUnits], [p].[PPM_FKEY_PRI_DESC] AS 
    [PpmFkeyPriDesc], [t].[jt_title] AS [JtTitle], [p].[PPM_FKEY_AR_SEQ] AS [AssetId], [t0].[AR_DESCRIPTION] AS [AssetDescription], 
    [p].[Deleted] AS [IsDeleted], ROW_NUMBER() OVER(ORDER BY [PpmFreq]) AS [__RowNumber__]
    FROM [PPMs] AS [p]
    LEFT JOIN (
        SELECT [j].*
        FROM [JobTypes] AS [j]
    ) AS [t] ON [p].[PPM_FKEY_IN_SEQ] = [t].[jt_seq]
    LEFT JOIN (
        SELECT [a].*
        FROM [Assets] AS [a]
    ) AS [t0] ON [p].[PPM_FKEY_AR_SEQ] = [t0].[ar_seq]
    WHERE (([p].[PPM_FKEY_BG_SEQ] = 172)) AND ((CHARINDEX(N'audit', [t].[jt_title]) > 0) 
    AND (CHARINDEX(N'df', [t0].[AR_DESCRIPTION]) > 0))
) AS [t1]
WHERE ([t1].[__RowNumber__] > 0) AND ([t1].[__RowNumber__] <= (50))

这看起来是我们的问题来自哪里,因为我们可以稍微修改它以从数据库中获得正确的结果:

ROW_NUMBER() OVER(ORDER BY [PpmFreq]) AS [__RowNumber__]

如果我们要修改上面的语句,同时也将表别名包含在[p].[PPM_FREQ]中,就像这样:ROW_NUMBER() OVER(ORDER BY [p].[PPM_FREQ]) AS [__RowNumber__]那么我们的问题就解决了,但是我们当前的linq查询似乎无法做到这一点。 / p>

2 个答案:

答案 0 :(得分:0)

查看以下内容是否更有效:

            var source = (from p in _ppmRepository.GetAll()
                         join jt in _jobTypeRepository.GetAll() on p.PpmFkeyInSeq equals jt.Id into jtdata
                         from jt in jtdata.DefaultIfEmpty()
                         join a in _assetRepository.GetAll() on p.PpmFkeyArSeq equals a.Id into aData
                         from a in aData.DefaultIfEmpty()
                         select new BuildingPpmListViewModel
                         {
                             PpmId = p.Id,
                             PpBgSeq = p.PpmFkeyBgSeq,
                             PpmFreq = p.PpmFreq,
                             PpmNextService = p.PpmNextService,
                             TotalCost = p.TotalCost,
                             PpmPeriodUnits = p.PpmPeriodUnits,
                             PpmFkeyPriDesc = p.PpmFkeyPriDesc,
                             JtTitle = jt.JtTitle,
                             AssetId = p.PpmFkeyArSeq,
                             AssetDescription = a.AssetDescription,
                             IsDeleted = p.IsDeleted

                         })
                         .Where(x =>  x.PpBgSeq == bldId)
                         .OrderBy(x => x.PpmFreq)
                         .ToList();

答案 1 :(得分:0)

这是我们之后直接向ef核心团队提交的已知问题。

  

这是一个已知问题,已针对即将发布的2.1版本进行了修复   您可以在此处查看更多详细信息和可能的解决方法   github.com / ASPNET / EntityFrameworkCore /问题/ 9535`   Smit Patel

如果您运行每晚构建,则可以解决上述问题。