亚音速查询,我烂了!

发布于 2024-09-25 12:52:58 字数 2531 浏览 1 评论 0原文

我在我的一个项目中使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

许你一世情深 2024-10-02 12:52:58

如果所有项目都通过文章 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;

If all items are tied together by article ID, just load them all once and sort them in memory.

//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;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文