Reputation: 50345
I have a table called BlogPost which has a 1-to-many relationship with the Comment table. (In Comment, there's a foreign key BlogPostId.)
Now I want to retrieve all posts as well as the latest comments of each post. I've tried with s/t like below but it doesn't work.
from r in Db.BlogPost
select new {Post = r, LatestComment = r.Comments.Last()};
The error message sounds like Last() is not a supported operator by EF. Is there any way to handle this?
Upvotes: 2
Views: 236
Reputation: 1062770
How about r.Comments.OrderByDescending(x=>x.Id).FirstOrDefault() ?
Essentially, order it (most recent first), and then take the first?
Upvotes: 2