下面出现错误
LINQ to Entities does not recognize the method 'System.Object get_Item(System.String)' method, and this method cannot be translated into a store expression
尝试从数据库获取 Postobject 的数据并将其发送为 Json 格式。 PostComments是Post的一对多关系。 我首先使用 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();
}
最佳答案
我不确定你是否已经尝试过这个。
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);
关于c# - LINQ to Entities 无法识别方法 'System.Object get_Item(System.String)',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15556422/