【问题标题】:LINQ to Entities does not recognize the method 'System.Object get_Item(System.String)'LINQ to Entities 无法识别方法“System.Object get_Item(System.String)”
【发布时间】: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


【解决方案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);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-08-18
    • 1970-01-01
    • 2013-04-07
    • 2012-04-22
    • 1970-01-01
    • 2017-01-02
    • 1970-01-01
    相关资源
    最近更新 更多