2009-05-31 89 views
3

我在編寫這個論壇,因爲我是LINQ的新手,當用戶點擊主頁面時遇到了這個問題。我想表顯示論壇的列表如下:LINQ查詢論壇

Forum --- Topics (count) --- Posts (count) --- LastPostUserId --- LastPostTime 

我有以下SQL表:

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) 

謝謝...

回答

1

因爲如果你使用的會員,你不希望在您的dbml的aspnet_Users顯示用戶名:

... 
LastPostUserId = posts.OrderByDescending(p=>p.PostId).Take(1).Select(p=> Membership.GetUser(p.UserId)) 
... 

另一個變化,使您發佈的樣品好一點是添加orderbydescending在帖子變量: 然後你可以從SELECT子句降4次重複OrderByDescending:

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) 
} 

甚至清潔劑:

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 
} 

測試時,有沒有最新帖子壽這段代碼,我認爲,如果採取(1)爲null,則可能會引發錯誤..

2
from forum in forums 
from posts in db.ForumPosts.Where(p => p.Thread.ForumId.Equals(forum.ForumId)) 
select new 
{ 
Forum = forum.Title, 
Topics = forum.ForumThreads.Count(), 
Posts = posts.Count(), 
LastPostBy = posts.OrderByDescending(p => p.CreateDate).FirstOrDefault(p => p.UserId), 
LastPostTime= posts.Max(p => p.CreateDate)) 
} 

未經測試ofcourse,但嘗試從這裏開始並檢查它執行的SQL查詢,並讓我知道它是否需要優化。

0

這幾乎是卓有成效(儘管它會產生一些可怕的SQL; - )...)

from forum in Forums 
let posts = ForumPosts.Where(p => p.ForumThreads.ForumId.Equals(forum.ForumId)) 
select new 
{ 
    Forum = forum.Title, 
    Description = forum.Description, 
    Topics = forum.ForumThreads.Count(), 
    Posts = posts.Count(), 
    LastPostId = posts.OrderByDescending(p=>p.PostId).Take(1).Select(p=>p.PostId), 
    LastPostThreadId = posts.OrderByDescending(p=>p.PostId).Take(1).Select(p=>p.ThreadId), 
    LastPostUserId = posts.OrderByDescending(p=>p.PostId).Take(1).Select(p=>p.UserId), 
    LastPostTime = posts.OrderByDescending(p=>p.PostId).Take(1).Select(p=>p.CreateDate) 
} 

最後一件事 - 我有一個從SQL表「ForumPosts」到「Aspnet_Users」的關係,我會喜歡將Aspnet_Users.UserName列顯示爲LastPostUserName ...怎麼做?你將如何優化整個查詢?

+0

的Aspnet_Users添加到您的dbml和p.aspnet_user.Name應該只是工作,沒有? – 2009-05-31 14:34:33

+0

和優化查詢,你可以複製粘貼生成的SQL? – 2009-05-31 14:34:55