I’m coding this forum and since I’m new to LINQ I ran into this problem when the user hits the main page. I want a table displaying a list of forums like this:
Forum --- Topics (count) --- Posts (count) --- LastPostUserId --- LastPostTime
I have the following SQL tables:
Forums:
ForumId (int32),
Title (string),
Description (string)
ForumThreads:
ThreadId (int32),
ForumId (int32),
UserId (guid),
Subject (string),
Views (int32),
CreateDate (DateTime)
ForumPosts:
PostId (int32),
ThreadId (int32),
UserId (guid),
Post (string),
CreateDate (datetime)
Thank you…
For displaying the user’s name if you use membership and you don’t want to include the aspnet_Users in your dbml:
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:
Or even cleaner:
Test this code when there are no last posts tho, I think it might throw an error if Take(1) is null..