c# - LINQ to Entities 无法识别方法 'System.Object get_Item(System.String)'

标签 c# asp.net-mvc asp.net-mvc-3 linq entity-framework

下面出现错误

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/

相关文章:

C# 屏蔽数组以排除索引,速度与 Python 一样快

c# - ASP.Net MVC 5 获取属性路由值

c# - .net开发代码结构-Controllers, Services, Repositories & Contexts

javascript - MVC3 Html.TextBoxFor 内容更改时提交表单

c# - Console.ReadLine() 替代方案

c# - 如何使基类方法的属性应用于继承类?

c# - SQLite : how? 中的 SHA1 哈希

相当于相对路径的 JavaScript

asp.net - 如何在 .net web api Controller 中读取多部分表单数据

javascript - 如何将 JavaScript 与 Razor 代码混合使用