亚音速查询,我烂了!
我在我的一个项目中使用 subsonic 2.2。我有一个评论部分,我在其中查询一个名为“评论”的表。首先我查询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);
}
}
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);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果所有项目都通过文章 ID 绑定在一起,只需将它们全部加载一次并在内存中对它们进行排序即可。
If all items are tied together by article ID, just load them all once and sort them in memory.