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),但它仍然沒有排序查詢。我是否犯了一些我沒有看到的錯誤,或者是一些實體框架問題?實體框架orderby不命令查詢結果
編輯:添加DB值
標識RID評論UID DATE
107 680試驗27 2017年8月24日10:49:41.583 1
108 680 gdfg 27 2017年8月24日10:50:06.630 1
109 681個Khgs gdlkdg 18 2017年8月24日12:08:01.793 1
110 680 TTT 27 2017年8月24日13:24:52.407 1
111 684 dasdasd 27 2017年8月24日13:32:22.997 1
112 680 fdsfs 27 2017年8月24日13:59:24.317 1
113 684 OK 27 2017年8月25日07:35:43.627 1
114 684 Ghfgjn 20 2017年8月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
我終於解決PROBL EM。我不知道是什麼導致了問題,但返回對象而不是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;
我喜歡你將查詢語法與方法語法混合在一起。 – Valkyrie
你能提供你在數據庫中的數據和你得到的結果嗎? –
你確定'Date'列是否有值,還是都是NULL?請包括一些示例行。 –