LINQ查询 - 奇怪的结果 - OrderBy

时间:2010-02-24 22:21:26

标签: sql linq

我有以下代码:

int pageNumber = 0;
int pageCount = 10;
int numberOfRecords = 10;

    var query = V_ers_notice.Skip(pageNumber * pageCount).Take(numberOfRecords );

return query.OrderByDescending(id => id.DOC_DATE).Select(ERSNoticeMap.DataToObject).ToList());

ERSNoticeMap代码如下:

public class ERSNoticeMap
{
        public static readonly Func<V_ers_notice, OGeneralFile> DataToObject = mapper =>
                                          new OGeneralFile
                                          {
                                              Docket = mapper.DOCKET,
                                              DocumentDate = mapper.DOC_DATE,
                                              FiledDate = mapper.FILED_DATE,
                                              FileGuid = mapper.FGUID,
                                              FileName = mapper.FName,
                                              FilePath = mapper.FPathName,
                                              FileSize = mapper.FSize,
                                              FileType = mapper.FType,
                                              Issuance = mapper.ISSUANCE,
                                              Title = mapper.TITLE,
                                              FileId = mapper.FID,
                                          };
}

我的OGeneralFile如下(即使它与此问题无关):

public class OGeneralFile
    {
        public int FileId { get; set; }
        public Guid FileGuid { get; set; }
        public string FilePath { get; set; }
        public string FileName { get; set; }
        public string FileType { get; set; }
        public string FileSize { get; set; }
        public string Issuance { get; set; }
        public DateTime? DocumentDate { get; set; }
        public DateTime? FiledDate { get; set; }
        public string Docket { get; set; }
        public string Title { get; set; }
        public string Type { get; set; }
    }

现在,这是一个奇怪的部分。当我运行它并查看SQL事件探查器时,它没有正确地命令我的查询。我得到以下内容:

SELECT [t1].[fID] AS [FID], [t1].[fGUID] AS [FGUID], [t1].[fPathName] AS [FPathName], [t1].[fName] AS [FName], [t1].[fType] AS [FType], [t1].[fSize] AS [FSize], [t1].[ISSUANCE], [t1].[DOC_DATE], [t1].[FILED_DATE], [t1].[DOCKET], [t1].[TITLE]
FROM (
    SELECT TOP (10) [t0].[fID], [t0].[fGUID], [t0].[fPathName], [t0].[fName], [t0].[fType], [t0].[fSize], [t0].[ISSUANCE], [t0].[DOC_DATE], [t0].[FILED_DATE], [t0].[DOCKET], [t0].[TITLE]
    FROM [v_ers_notice] AS [t0]
    ) AS [t1]
ORDER BY [t1].[DOC_DATE] DESC

当选择不在外面的前10名时,顺序应该在内部查询中。

对于为什么会发生这种情况的任何想法?

谢谢,

1 个答案:

答案 0 :(得分:7)

我认为你的查询错了。你需要先OrderBy才能跳过并拍摄,而不是之后:

var query = V_ers_notice.OrderByDescending(id => id.DOC_DATE)
                        .Skip(pageNumber * pageCount)
                        .Take(numberOfRecords );