2017-03-22 34 views
-1

我正在使用LINQ查詢,我正在按升序列出項目。我需要從列表中獲取最新的4個項目。從列表中獲取最新/最後一項,升序

我有加載更多按鈕,onclick應該從列表中獲得前4項。

var GetALLFields =(from post in dbContext.PostTable 
               join com in dbContext.CommentTable on post.PostID equals com.PostedId 
               join user in dbContext.UserTable on com.commenterID equals user.userId 
               where (com.PostedId == PostID) 

               select new 
               { 
                PostId = post.PostID, 
                commentId = com.commentID, 
                CommenterID = user.userId, 
                name = user.Name, 
                commenterUserName = user.userName, 
                Imagebase64 = user.userImageBase64, 
                ImageType = user.userImagetype, 
                commentId = post.PostedDataID, 
                       commentcreatedAt = com.createdAt, 
                commenterDelete = com.commenterDelete, 
               }); 


    if (GetALLFields != null) 
        { 
    //PageSize I am getting from ajax (first it be 0 then pageSize will be multple of 4) 
         GetALLComment = GetALLFields.OrderBy(x=>x.createdAt).Skip(PageSize).Take(4).Reverse().ToList(); 
         GetALLComment.Reverse(); 
        } 

的onpage負載,我需要從列表

7 
8 
9 
10 

的OnClick加載更多的獲得最新/最後的4個項目,我需要獲得以前的4個最新的4項

3 
    4 
    5 
    6 
    7 
    8 
    9 
    10 
沿項目
+0

所以你需要結果加起來嗎? –

+0

如果是這樣的話,你可以在你填寫的地方發佈ajax。那就是你需要編輯的地方!這段代碼正在做它的工作! –

+0

您的實際問題比 –

回答

0

您正在使用Reverse()方法兩次。從其中一個步驟移除Reverse()。 像這樣:

GetALLComment=GetALLFields.OrderBy(x=>x.createdAt).Skip(PageSize).Take(4).ToList(); 
GetALLComment.Reverse(); 
0

我不明白你爲什麼要倒車記錄你把他們後再次反轉他們,這兩個命令取消自己出。

如果你想在最新的記錄,第一次只使用OrderByDescending,如果你也想採取8項在第二重裝,你需要採取每頁* 4項

GetALLComment = GetALLFields.OrderByDescending(x=>x.createdAt).Skip(PageSize).Take(PageSize*4).ToList(); 

然後扭轉他們,讓他們有升序

GetALLComment.Reverse();