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;
    }
4

1 回答 1

4

解决方案就是从查询中删除“into z”部分,就这么简单!

正如@Nilesh 和@Gert Arnold 指出的那样

于 2013-08-04T14:28:09.560 回答