2016-12-01 49 views
0

在這裏,我得到的所有發佈和評論accordie friendID其工作正常,我的問題是當我調試它顯示我在GetAllPost 2或更多的數據,但它只返回最後的數據,我沒有得到爲什麼它不返回一次所有數據需要幫助..如何添加對象的列表並返回它

public dynamic getalldetails(int friendsid) 
{ 
    var GetAllPost = (dynamic)null; 
    FriendsId =dbContext.Usertable.where(u=>u.userID==friendsid).tolist(); 

    if(FriendsId!=null) 
    foreach(var item in FriendsId) 
    { 
     GetAllPost = (from post in db.Posts.where(item.userID==post.userID).ToList() 
         orderby post.PostedDate descending 
         select new 
         { 
          Message = post.Message, 
          PostedBy = post.PostedBy, 
          PostedByName = post.UserProfile.UserName, 
          PostedByAvatar =imgFolder + (String.IsNullOrEmpty(post.UserProfile.AvatarExt) ? defaultAvatar : post.PostedBy + "." + post.UserProfile.AvatarExt), 
          PostedDate = post.PostedDate, 
          PostId = post.PostId, 
          PostComments = from comment in post.PostComments.ToList() 
              orderby comment.CommentedDate 
              select new 
              { 
               CommentedBy = comment.CommentedBy, 
               CommentedByName = comment.UserProfile.UserName, 
               CommentedByAvatar = imgFolder +(String.IsNullOrEmpty(comment.UserProfile.AvatarExt) ? defaultAvatar : comment.CommentedBy + "." + comment.UserProfile.AvatarExt), 
               CommentedDate = comment.CommentedDate, 
               CommentId = comment.CommentId, 
               Message = comment.Message, 
               PostId = comment.PostId 
              } 
         }).AsEnumerable(); 

    } 
    return GetAllPost; 
} 
+0

很難讀取您的代碼。你能否包含適當的符號;和點? –

+1

給出的代碼甚至不能正確編譯,請先修復。如果你讓我們難以幫助,不要指望我們的幫助。 –

回答

0

每循環一次,你分配給GetAllPost,從而扔掉不管它之前舉行。您需要積累帖子 - 沿着這些線:

ArrayList GetAllPost = new ArrayList(); 
... 
foreach (var item in FriendsId) 
{ 
    GetAllPost.AddRange(from post in 
     // Your LINQ code 
     ...); 
} 

return GetAllPost; 
相關問題