作爲學校項目的一部分我試圖構建一個簡單的論壇。我無法找到任何與我的風格完美融合的產品,因此我可以自由製作基礎知識。在一個函數中對單個模型運行多個linq查詢
使用列表我將輸出表單,然後線程,然後張貼,所以它會去3頁深。
我遇到的問題是在第一頁上我想從帖子/線程的數據庫中獲取當前計數。因爲這可以在一瞬間通知我可以改變,我想我會計算在頁面加載,因爲這個項目充其量可能有100個記錄從...
下面的代碼會引發錯誤。
public ActionResult Index(int page = 1)
{
ViewBag.Title = "Forums";
var model = db.forums.OrderBy(x=> x.forumID).ToPagedList(page, 15);
foreach (var m in db.forums)
{
m.postCount = db.threads.Where(t => t.forumID == m.forumID && t.threadID == t.parentThreadID).Count();
m.threadCount = db.threads.Where(t => t.forumID == m.forumID).Count();
}
return View(model);
}
錯誤時,拋出
Exception Details: System.InvalidOperationException: There is already an open DataReader associated with this Command which must be closed first.
Source Error:
Line 20: foreach (var m in db.forums)
Line 21: {
Line 22: m.postCount = db.threads.Where(t => t.forumID == m.forumID && t.threadID == t.parentThreadID).Count();
Line 23: m.threadCount = db.threads.Where(t => t.forumID == m.forumID).Count();
Line 24: }