Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>For displaying the user's name if you use membership and you don't want to include the aspnet_Users in your dbml:</p> <pre><code>... LastPostUserId = posts.OrderByDescending(p=&gt;p.PostId).Take(1).Select(p=&gt; Membership.GetUser(p.UserId)) ... </code></pre> <p>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:</p> <pre><code>from forum in Forums let posts = ForumPosts.Where(p =&gt; p.ForumThreads.ForumId.Equals(forum.ForumId)).OrderByDescending(p=&gt;p.PostId) select new { Forum = forum.Title, Description = forum.Description, Topics = forum.ForumThreads.Count(), Posts = posts.Count(), LastPostId = posts.Take(1).Select(p=&gt;p.PostId), LastPostThreadId = posts.Take(1).Select(p=&gt;p.ThreadId), LastPostUserId = posts.Take(1).Select(p=&gt;p.UserId), LastPostTime = posts.Take(1).Select(p=&gt;p.CreateDate) } </code></pre> <p>Or even cleaner:</p> <pre><code>from forum in Forums let posts = ForumPosts.Where(p =&gt; p.ForumThreads.ForumId.Equals(forum.ForumId)) let lastPost = posts.OrderByDescending(p=&gt;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 } </code></pre> <p>Test this code when there are no last posts tho, I think it might throw an error if Take(1) is null..</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload