2017-08-28 75 views
0
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; 
+1

我喜歡你將查詢語法與方法語法混合在一起。 – Valkyrie

+0

你能提供你在數據庫中的數據和你得到的結果嗎? –

+0

你確定'Date'列是否有值,還是都是NULL?請包括一些示例行。 –

回答

0

您是否嘗試過.OrderBy後選擇,而不是之前?

+0

在LINQ中做選擇不會改變順序。 –

+0

我試過它也不起作用。 – kirkharamiler

+0

您可以共享該表的CREATE TABLE腳本 –

0

如果它是空的,嘗試的OrderBy p.Date.GetDefaultOrValue()

+0

它不是可以爲空的DateTime類型。這是DateTime。我檢查了數據庫所有評論有日期值 – kirkharamiler

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; 
0

將計算時間轉換爲列表兩次是浪費時間。此外,如果您僅使用包含結果的某些部分,則不包括。實體框架將知道要查詢的列。

話雖如此。我認爲你的問題是由於方法語法和查詢語法的混合。也許增加一些括號會有所幫助。

我已將您的查詢翻譯成方法語法,並使用您的輸入。產量如預期。

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(或者把它放在一個列表中)

+0

它不起作用。它適用於如果我返回類型對象。但是如果我返回CommentDTO它仍然沒有排序。奇怪的問題我不知道爲什麼DTO類導致問題 – kirkharamiler