2
votes

getting error below

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

Trying to get data from db for Postobject and send it to Json format. PostComments is 1 to many relation of Post. i am using EF 5.x code first.

    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();
        }
1
The code you've posted is correct - is there anything else going on? - Nick Butler
Try putting .ToList() before , JsonRequestBehavior.AllowGet to force the evaluation earlier to try to figure out what part that is failing. - Albin Sunnanbo
i get the same error when i add ToList 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. - Justin Homes

1 Answers

1
votes

I am not sure if you have already tried this.

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);