【发布时间】:2013-03-21 19:30:47
【问题描述】:
下面出现错误
LINQ to Entities 无法识别方法“System.Object” get_Item(System.String)' 方法,这个方法不能翻译 变成商店表达式
尝试从数据库中获取 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();
}
【问题讨论】:
-
您发布的代码是正确的 - 还有其他情况吗?
-
尝试将
.ToList()放在, JsonRequestBehavior.AllowGet之前以强制执行更早的评估以尝试找出失败的部分。 -
当我添加 ToList LINQ to Entities 时遇到同样的错误,无法识别方法 'System.Object get_Item(System.String)' 方法,并且此方法无法转换为存储表达式。
标签: c# asp.net-mvc asp.net-mvc-3 linq entity-framework