我在我的一个项目中使用亚音速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);
}
}
答案 0 :(得分:0)
如果所有项目都通过文章ID绑定在一起,只需将它们加载一次并在内存中进行排序。
//Load all article comments from the Database
DAL.CommentCollection dbComments = new Select()
.From(DAL.Comment.Schema)
.Where(DAL.Comment.ArticleIDColumn).IsEqualTo(aid)
.And(DAL.Comment.IsActiveColumn).IsEqualTo(true)
.And(DAL.Comment.IsDeletedColumn).IsEqualTo(false)
.OrderBy(DAL.Comment.Columns.ParentID, DAL.Comment.Columns.CreatedOn)
.ExecuteAsCollection<DAL.CommentCollection>();
//Now sort all comments however needed
DAL.CommentCollection parents = new DAL.CommentCollection();
foreach (DAL.Comment parent in dbComments)
{
if (parent.ParentID == 0)
{
parents.add(parent);
}
else
break; //because of the way we ordered the items above, all parents should now be processed
}
//now loop through the parent collection and add children beneath parents to the final collection
foreach (DAL.Comment parent in parents)
{
comments.add(parent);
foreach (DAL.Comment possibleChild in dbComments)
{
if (possibleChild.ParentID > 0 && possibleChild.ParentID == parent.ID)
{
comments.add(possibleChild);
}
}
}
return comments;