2013-03-21 105 views
2

下面LINQ到實體無法識別方法 'System.Object的get_Item(System.String)'

LINQ得到錯誤到實體無法識別方法「System.Object的 get_Item(System.String) '方法,和這種方法不能被翻譯成 商店表達

試圖從分貝數據Postobject並將其發送到JSON格式。 帖子評論是Post to many的關係。 我首先使用EF 5.x代碼。

try 
     { 
      IEnumerable<Post> userPosts; 
      userPosts = (from q in db.Posts 
         where q.UserId == userId 
         && q.PostId > postid 
         select q).Take(5); 


      return Json(userPosts.Select(x => new 
      { 
       success = 1, 
       contenttext = x.PostContent, 
       postId = x.PostId, 
       comments = x.PostComments //is a child collection object 
      }), JsonRequestBehavior.AllowGet); 

     } 
     catch (Exception ex) 
     { 
      return Json(new { success = 0 }); 

     } 
     finally 
     { 
      //db.Dispose(); 
     } 
+0

您發佈的代碼是正確的 - 還有其他事情嗎? – 2013-03-21 19:36:00

+0

嘗試在',JsonRequestBehavior.AllowGet'之前放置'.ToList()'來強制評估以試圖找出失敗的部分。 – 2013-03-21 19:46:06

+0

我收到相同的錯誤,當我添加ToList LINQ to Entities不能識別方法'System.Object get_Item(System.String)'方法,並且此方法不能轉換爲存儲表達式。 – 2013-03-21 22:10:48

回答

1

我不知道你是否已經試過這個。

return Json(userPosts.Select(x => new 
     { 
      success = 1, 
      contenttext = x.PostContent, 
      postId = x.PostId, 
      comments = x.PostComments //is a child collection object 
     }).ToList(), JsonRequestBehavior.AllowGet); 
相關問題