For displaying the user's name if you use membership and you don't want to include the aspnet_Users in your dbml:
...
LastPostUserId = posts.OrderByDescending(p=>p.PostId).Take(1).Select(p=> Membership.GetUser(p.UserId))
...
Another change to make your posted sample a bit better is to add the orderbydescending in the posts variable:
Then you can drop the 4 times repeated OrderByDescending from the select clause:
from forum in Forums
let posts = ForumPosts.Where(p => p.ForumThreads.ForumId.Equals(forum.ForumId)).OrderByDescending(p=>p.PostId)
select new
{
Forum = forum.Title,
Description = forum.Description,
Topics = forum.ForumThreads.Count(),
Posts = posts.Count(),
LastPostId = posts.Take(1).Select(p=>p.PostId),
LastPostThreadId = posts.Take(1).Select(p=>p.ThreadId),
LastPostUserId = posts.Take(1).Select(p=>p.UserId),
LastPostTime = posts.Take(1).Select(p=>p.CreateDate)
}
Or even cleaner:
from forum in Forums
let posts = ForumPosts.Where(p => p.ForumThreads.ForumId.Equals(forum.ForumId))
let lastPost = posts.OrderByDescending(p=>p.PostId).Take(1)
select new
{
Forum = forum.Title,
Description = forum.Description,
Topics = forum.ForumThreads.Count(),
Posts = posts.Count(),
LastPostId = lastPost.PostId,
LastPostThreadId = lastPost.ThreadId,
LastPostUserId = lastPost.UserId,
LastPostUserName = Membership.GetUser(lastPost.UserId),
LastPostTime = lastPost.CreateDate
}
Test this code when there are no last posts tho, I think it might throw an error if Take(1) is null..