Hi all,
I'm using subsonic 2.2 in one of my projects. I'm have a comment section where i query one table called Comments. First I query all records with ParentId=0, and then in the foreach statement I query all records with ParentId=currentRecord.Id. Now I know this is a bad habit but I don't know how to get around this in a single query using SubSonic, maybe I'm missing something important here.
I hope someone can point me in the right direction. Thank you for your time!
Kind regards, Mark
[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);
}
}