0

I'm trying to list every comments created by the the specific user (Users Id stored in Session["LoggedUserID"]) but I'm having hard time solving the lambda expression as I basically never done it before.

The following code is faulty, but this about what I had in mind.

 public ActionResult MyComments(Guid? id)
        {
            id = new Guid(Session["LoggedUserID"].ToString());

            var comments = db.Comments.Include(m=> m.CreatorUserId.Equals(id));

            return View(comments.ToList());

        }

Addintional info:

  • Database-first

  • Using Guids for Id's.

Any help is much appreciated.

1
  • 1
    You are using Include instead of Where, include should be used when you want to expand an entity inside your comments, ex: the user CreatorUser Commented Sep 27, 2016 at 5:59

2 Answers 2

2

It will be more helpful if you would publish your relevant entities SQL scheme, but this should suffice.

  1. I've used LINQ to Entities .Where method in order to filter only the one matching the given id.
  2. Include is not needed, because it is used for loading relative entities, which is not needed for Comments table.

Code:

public ActionResult MyComments(Guid? id)
{
    id = Guid.Parse(Session["LoggedUserID"].ToString());

    var review = db.Comments.Where(m => m.CreatorUserId.Equals(id));
    var result = review.ToList(); 

    return View(result);
}
Sign up to request clarification or add additional context in comments.

Comments

2

Try this instead of include,

db.Comments.Where(m=> m.CreatorUserId == id);

Hope helps,

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.