【发布时间】:2010-10-15 15:41:52
【问题描述】:
我在我的一个项目中使用 subsonic 2.2。我有一个评论部分,我在其中查询一个名为 Comments 的表。首先查询 ParentId=0 的所有记录,然后在 foreach 语句中查询 ParentId=currentRecord.Id 的所有记录。现在我知道这是一个坏习惯,但我不知道如何使用 SubSonic 在单个查询中解决这个问题,也许我在这里遗漏了一些重要的东西。
我希望有人能指出我正确的方向。感谢您的宝贵时间!
亲切的问候, 标记
[WebMethod]
public List<Comment> GetComments(int aid)
{
DAL.CommentCollection coll = new DAL.CommentCollection();
SubSonic.Query qry = new SubSonic.Query(DAL.Comment.Schema);
qry.AddWhere(DAL.Comment.Columns.ArticleID, aid);
qry.AddWhere(DAL.Comment.Columns.ParentID, 0);
qry.AddWhere(DAL.Comment.Columns.IsActive, true);
qry.AddWhere(DAL.Comment.Columns.IsDeleted, false);
qry.ORDER_BY(DAL.Comment.Columns.CreatedOn, "Asc");
qry.PageSize = Classes.Settings.Controls.Comments.GetCommentsPerPage();
coll.LoadAndCloseReader(qry.ExecuteReader());
foreach (DAL.Comment item in coll)
{
Comment c = new Comment();
c.Date = Convert.ToDateTime(item.CreatedOn).ToLongDateString();
c.UserName = item.User.UserName;
c.FullText = item.FullText;
c.Gravatar = Classes.Data.HashString(item.User.GravatarId);
c.IsSub = false;
c.CommentId = (int)item.CommentID;
comments.Add(c);
//Get replies
GetReplies((int)item.CommentID, aid);
}
return comments;
}
private void GetReplies(int CommentId, int aid)
{
DAL.CommentCollection coll = new DAL.CommentCollection();
SubSonic.Query qry = new SubSonic.Query(DAL.Comment.Schema);
qry.AddWhere(DAL.Comment.Columns.ArticleID, aid);
qry.AddWhere(DAL.Comment.Columns.ParentID, CommentId);
qry.AddWhere(DAL.Comment.Columns.IsActive, true);
qry.AddWhere(DAL.Comment.Columns.IsDeleted, false);
qry.ORDER_BY(DAL.Comment.Columns.CreatedOn, "Asc");
qry.PageSize = Classes.Settings.Controls.Comments.GetCommentsPerPage();
coll.LoadAndCloseReader(qry.ExecuteReader());
foreach (DAL.Comment item in coll)
{
Comment c = new Comment();
c.Date = Convert.ToDateTime(item.CreatedOn).ToLongDateString();
c.UserName = item.User.UserName;
c.FullText = item.FullText;
c.Gravatar = Classes.Data.HashString(item.User.GravatarId);
c.IsSub = true;
c.CommentId = (int)item.CommentID;
comments.Add(c);
}
}
【问题讨论】:
-
我已经添加了上面的代码,谢谢
标签: subsonic