2

I am really poor at LINQ and can't figure out a simple problem. I have a MVC Web API that has a controller. I have a method inside the controller to return back data for comments entered by user for an item.

The data structure is simple - Comments and User tables with UserID column acting as the foreign key

To solve the problem, I have the following method, which has a LINQ query to do a join between Comments and User tables and return back an object in a new extended object that combines the Comments and User details. I cant seem to grab data from the User table. Can someone please help?

public IQueryable<CommentsWithUserDetails> GetReviewsWithUserByItem(int ID)
    {
        var query = from x in db.Comments
                    join y in db.Users on x.CommentsUserID equals y.UserID into z
                    where x.CommentsItemID.Equals(ID)
                    select new CommentsWithUserDetails
                    {
                        CommentsUserID = x.CommentsUserID,
                        CommentsText = x.CommentsText,
                        CommentsRating = x.CommentsRating,
                        CommentsDate = x.CommentsDate,
                        UserFirstName = y.FirstName,
                        UserLastName = y.LastName,
                        UserPictureURL = y.PictureURL
                    };

        return query;
    }
5
  • 1
    You joined everything into z, so use z instead of x! Commented Aug 3, 2013 at 15:40
  • you can also remove into z Commented Aug 3, 2013 at 16:12
  • actually...why do you even have the where clause. Web API allows you to use OData query strings to apply a where "filter". Commented Aug 3, 2013 at 18:05
  • Just remove the into z, unless you want an outer join. Commented Aug 3, 2013 at 18:14
  • thanks guys, it is as simple as removing the into z from the query! Commented Aug 4, 2013 at 14:26

1 Answer 1

4

The solution is just to remove the 'into z' part from the query, yes as simple as that!

As pointed by @Nilesh and @Gert Arnold

Sign up to request clarification or add additional context in comments.

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.