实体框架orderby不排序查询结果

时间:2017-08-28 15:56:13

标签: c# asp.net entity-framework

var comments =  (from p in _ctx.CommentRottas
                                .Where(s => s.Rotta_Id == Id && s.Status == 1)
                                .Include(s => s.Client)                              
                            orderby p.Date descending
                            select new CommentDTO
                                {
                                    CommentId = p.Id,
                                    Rotta = new RottaDTO
                                    {
                                        RottaId = p.Rotta_Id,
                                        RottaDate = p.SU_ROUTES.Date,
                                        ClientId = p.SU_ROUTES.ClientId,
                                        COMMENTS_NUM = p.SU_ROUTES.COMMENTS_NUM,
                                        LIKES_NUM = p.SU_ROUTES.LIKES_NUM,
                                    },
                                    Client = new ClientDTO
                                    {
                                        Id = p.Client_Id,
                                        UserName = p.Client.UserName,
                                        Profile_Image = p.Client.Profile_Image,
                                    },
                                    CommentDate = p.Date,
                                    Comment = p.comment,
                                }
                            )
                            .ToList();

            return comments;
你好。我试图按日期降序排序评论。但无论我尝试过什么都没有订购。我有一个类似的查询,它的工作原理。但是这个没有。我也尝试使用OrderByDescending(m => m.CommentDate),但它仍然没有命令查询。我是否犯了一些我没有看到的错误,或者它是一些实体框架问题?

编辑:添加了数据库值

ID RID评论UID DATE

107 680 test 27 2017-08-24 10:49:41.583 1

108 680 gdfg 27 2017-08-24 10:50:06.630 1

109 681 Khgs gdlkdg 18 2017-08-24 12:08:01.793 1

110 680 ttt 27 2017-08-24 13:24:52.407 1

111 684 dasdasd 27 2017-08-24 13:32:22.997 1

112 680 fdsfs 27 2017-08-24 13:59:24.317 1

113 684 OK 27 2017-08-25 07:35:43.627 1

114 684 Ghfgjn 20 2017-08-25 13:43:15.020 1

来自RID 684查询的结果:

[
    {
        "CommentId": 111,
        "CommentDate": "2017-08-24T13:32:22.997",
        "Comment": "dasdasd",
        "Client": {
            "Id": 27,
            "UserName": "Test",
            "Profile_Image": "https://rota2.blob.core.windows.net/profile-images/profile.png"
        },
        "Station": null,
        "Rotta": {
            "RottaId": 684,
            "RottaDate": "2017-08-24T13:30:40.51",
            "COMMENTS_NUM": 3,
            "LIKES_NUM": 2,
            "Completed": 0,
            "STATUS": 0,
            "Is_Started": null,
            "ClientId": 19,
            "Stations": null,
            "Client": null
        },
        "Media": null
    },
    {
        "CommentId": 114,
        "CommentDate": "2017-08-25T13:43:15.02",
        "Comment": "Ghfgjn",
        "Client": {
            "Id": 20,
            "UserName": "Besart2",
            "Profile_Image": "https://rota2.blob.core.windows.net/profile-images/Date-2017-08-28T08-06-21-User-20.jpg"
        },
        "Station": null,
        "Rotta": {
            "RottaId": 684,
            "RottaDate": "2017-08-24T13:30:40.51",
            "COMMENTS_NUM": 3,
            "LIKES_NUM": 2,
            "Completed": 0,
            "STATUS": 0,
            "Is_Started": null,
            "ClientId": 19,
            "Stations": null,
            "Client": null
        },
        "Media": null
    },
    {
        "CommentId": 113,
        "CommentDate": "2017-08-25T07:35:43.627",
        "Comment": "OK",
        "Client": {
            "Id": 27,
            "UserName": "Test",
            "Profile_Image": "https://rota2.blob.core.windows.net/profile-images/profile.png"
        },
        "Station": null,
        "Rotta": {
            "RottaId": 684,
            "RottaDate": "2017-08-24T13:30:40.51",
            "COMMENTS_NUM": 3,
            "LIKES_NUM": 2,
            "Completed": 0,
            "STATUS": 0,
            "Is_Started": null,
            "ClientId": 19,
            "Stations": null,
            "Client": null
        },
        "Media": null
    }
]




GO

/****** Object:  Table [dbo].[CommentRottas]    Script Date: 8/28/2017 7:14:59 PM ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE TABLE [dbo].[CommentRottas](
    [Id] [bigint] IDENTITY(1,1) NOT NULL,
    [Rotta_Id] [bigint] NOT NULL,
    [comment] [nvarchar](500) NULL,
    [Client_Id] [int] NOT NULL,
    [Date] [datetime] NOT NULL,
    [Status] [int] NOT NULL,
 CONSTRAINT [PK_dbo.CommentRottas] PRIMARY KEY CLUSTERED 
(
    [Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON)
)
GO

ALTER TABLE [dbo].[CommentRottas]  WITH CHECK ADD  CONSTRAINT [FK_dbo.CommentRottas_dbo.SU_ROUTES_Rotta_Id] FOREIGN KEY([Rotta_Id])
REFERENCES [dbo].[SU_ROUTES] ([ROUTE_ID])
ON DELETE CASCADE
GO

ALTER TABLE [dbo].[CommentRottas] CHECK CONSTRAINT [FK_dbo.CommentRottas_dbo.SU_ROUTES_Rotta_Id]
GO

我终于解决了问题。我不知道是什么原因引起了问题但是返回对象而不是DTO解决了问题。这是我的解决方案。

   public ICollection<Object> GetRouteComments(long Id)
    {
        /*
        var result = await _ctx.CommentRottas
                        .Where(s => s.Rotta_Id == Id)
                        .Include(s => s.Client)
                        .Where(s => s.Status == 1)
                        .OrderByDescending(p => p.Date)
                        .ToListAsync();
        */
        var comments = (from p in _ctx.CommentRottas
                           .Where(s => s.Rotta_Id == Id && s.Status == 1)
                           .Include(s => s.Client)
                        orderby p.Date descending
                        select new CommentDTO
                        {
                            CommentId = p.Id,
                            Rotta = new RottaDTO
                            {
                                RottaId = p.Rotta_Id,
                                RottaDate = p.SU_ROUTES.Date,
                                ClientId = p.SU_ROUTES.ClientId,
                                COMMENTS_NUM = p.SU_ROUTES.COMMENTS_NUM,
                                LIKES_NUM = p.SU_ROUTES.LIKES_NUM,
                            },
                            Client = new ClientDTO
                            {
                                Id = p.Client_Id,
                                UserName = p.Client.UserName,
                                Profile_Image = p.Client.Profile_Image,
                            },
                            CommentDate = p.Date,
                            Comment = p.comment,
                        }
                        )
                        .ToList().Cast<Object>().ToList();

        return comments;

4 个答案:

答案 0 :(得分:0)

您是否尝试过选择后.OrderBy而不是之前?

答案 1 :(得分:0)

如果它可以为空,请尝试命令p.Date.GetDefaultOrValue()

答案 2 :(得分:0)

我找到了解决方案,但我不确定它为什么会起作用。

 public ICollection<Object> GetRouteComments(long Id)
    {
        /*
        var result = await _ctx.CommentRottas
                        .Where(s => s.Rotta_Id == Id)
                        .Include(s => s.Client)
                        .Where(s => s.Status == 1)
                        .OrderByDescending(p => p.Date)
                        .ToListAsync();
        */
        var comments = (from p in _ctx.CommentRottas
                           .Where(s => s.Rotta_Id == Id && s.Status == 1)
                           .Include(s => s.Client)
                        orderby p.Date descending
                        select new CommentDTO
                        {
                            CommentId = p.Id,
                            Rotta = new RottaDTO
                            {
                                RottaId = p.Rotta_Id,
                                RottaDate = p.SU_ROUTES.Date,
                                ClientId = p.SU_ROUTES.ClientId,
                                COMMENTS_NUM = p.SU_ROUTES.COMMENTS_NUM,
                                LIKES_NUM = p.SU_ROUTES.LIKES_NUM,
                            },
                            Client = new ClientDTO
                            {
                                Id = p.Client_Id,
                                UserName = p.Client.UserName,
                                Profile_Image = p.Client.Profile_Image,
                            },
                            CommentDate = p.Date,
                            Comment = p.comment,
                        }
                        )
                        .ToList().Cast<Object>().ToList();

        return comments;

答案 3 :(得分:0)

将它转换为List两次是浪费计算时间。此外,如果您只使用包含结果的某些部分,请不要包含。实体框架将知道要查询的列。

说完了。我认为你的问题是由于方法语法和查询语法的混合。也许加一些额外的括号会有所帮助。

我已将您的查询翻译成方法语法,并使用了您的输入。输出符合预期。

int Id = 684;
var result = myDbContext.CommentRottas
    .Where(comment => comment.Id == Id && comment.Status == 1)
    .OrderByDescending(comment => comment.Date)
    .Select(comment => new CommentDTO()
    {
        CommentId = comment.Id,
        Rotta = new RottaDTO
        {
            RottaId = comment.Rotta_Id,
            // not tested all properties
        },
        Client = new ClientDTO()
        {
            Id = comment.Client_Id,
            // not tested all properties  
        },
        CommentDate = comment.Date,
        Comment = comment.Comment,
    })
    .ToList();

这有效并提供预期的顺序。所以我的建议是坚持一种语法:方法或查询。如果使用查询语法,请使用括号。

最后:考虑不返回列表,而是IEnumerable。如果您的调用者只想要第一个元素,或者前几个元素,那么查询所有元素将是一个浪费。让你的调用者决定是否要在枚举之前将其他函数添加到IEnumerable(或将其放入列表中)