简体   繁体   English

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

[英]Entity Framework orderby doesnt order query result

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;

Hi guys. 嗨,大家好。 I am trying to order comments in descending order by their date. 我正在尝试按日期降序排列评论。 But whatever I tried it does not order. 但是,无论我尝试了什么,都不会命令。 I have similar query to this one and it works. 我有与此类似的查询,它的工作原理。 But this one does not. 但这不是。 I also tried using OrderByDescending(m => m.CommentDate) but still it does not order query. 我也尝试使用OrderByDescending(m => m.CommentDate),但它仍然不对查询进行排序。 Do I make some mistake that I do not see or it's some entity framework issue ? 我会犯一些我看不到的错误,还是某些实体框架问题?

EDIT: Added DB Values 编辑:添加数据库值

Id RID Comment UID DATE ID RID评论UID DATE

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

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

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

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

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

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

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

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

Result From Query For RID 684: 查询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

I finally solved problem. 我终于解决了问题。 I dont know what caused problem but returning object instead of DTO solved problem. 我不知道是什么引起了问题,但是返回对象而不是DTO解决了问题。 This is my solution. 这是我的解决方案。

   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;

您是否尝试过在选择之后而不是之前拥有.OrderBy

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

I Found Solution, But i am not sure why it works. 我找到了解决方案,但是我不确定为什么能解决问题。

 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;

It is a waste of computing time to convert it to a List twice. 将其两次转换为List浪费了计算时间。 Besides don't ise Include if you are only using certain parts of the Include result. 此外,如果仅使用包括结果的某些部分,则不要包括。 Entity Framework will know which columns to query. 实体框架将知道要查询的列。

Having said that. 话说回来。 I think your problem is because of the mixing of method syntax and query syntax. 我认为您的问题是由于方法语法和查询语法的混合。 Maybe putting some extra parentheses would help. 也许加上一些括号会有所帮助。

I've translated your query into method syntax, and used your input. 我已经将您的查询翻译成方法语法,并使用了您的输入。 The output is as expected. 输出是预期的。

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();

This works and gives the expected order. 这可以正常工作并给出预期的顺序。 So my advise would be to stick to one syntax: method or query. 因此,我的建议是坚持一种语法:方法或查询。 If you use query syntax, be use parentheses. 如果使用查询语法,请使用括号。

Finally: consider not to return the list, but the IEnumerable. 最后:考虑不返回列表,而是返回IEnumerable。 If your caller only wants the first element, or the first few, It would be a wast to query everything. 如果您的呼叫者只想要第一个元素或前几个元素,那么查询所有内容将是一种浪费。 Let your caller decide if he wants to add other functions to your IEnumerable before enumerating over it (or put it in a list) 让您的调用者在枚举(或将其放在列表中)之前决定是否要向IEnumerable添加其他功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM