views:

192

answers:

1

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?

+2  A: 

How about r.Comments.OrderByDescending(x=>x.Id).FirstOrDefault() ?

Essentially, order it (most recent first), and then take the first?

Marc Gravell
Worked like charm, thanks!
Buu Nguyen