Ejemplo n.º 1
0
        private void FillDoingComments(DoingCollection doings, SqlSession db)
        {
            if (doings.Count == 0)
            {
                return;
            }

            List <int> minIds = new List <int>();
            List <int> maxIds = new List <int>();

            for (int i = 0; i < doings.Count; i++)
            {
                if (doings[i].TotalComments == 0)
                {
                    continue;
                }
                else if (doings[i].TotalComments == 1)
                {
                    minIds.Add(doings[i].DoingID);
                }
                else
                {
                    minIds.Add(doings[i].DoingID);
                    maxIds.Add(doings[i].DoingID);
                }
            }

            if (minIds.Count == 0)
            {
                return;
            }

            using (SqlQuery query = db.CreateQuery())
            {
                query.CommandText = @"
SELECT * FROM bx_Comments WHERE CommentID IN(
    SELECT Min(CommentID) FROM [bx_Comments] WHERE [Type]=3 AND IsApproved = 1 AND [TargetID] IN(@MinTargetIDs) GROUP BY TargetID
);
";
                if (maxIds.Count > 0)
                {
                    query.CommandText += @"
SELECT * FROM bx_Comments WHERE CommentID IN(
    SELECT Max(CommentID) FROM [bx_Comments] WHERE [Type]=3 AND IsApproved = 1 AND [TargetID] IN(@MaxTargetIDs) GROUP BY TargetID
);
";
                }

                query.CreateInParameter <int>("@MinTargetIDs", minIds);
                query.CreateInParameter <int>("@MaxTargetIDs", maxIds);

                using (XSqlDataReader reader = query.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        Comment comment = new Comment(reader);
                        Doing   doing   = doings.GetValue(comment.TargetID);
                        if (doing != null)
                        {
                            if (doing.CommentList.ContainsKey(comment.CommentID) == false)
                            {
                                doing.CommentList.Add(comment);
                            }
                        }
                    }
                    if (reader.NextResult())
                    {
                        while (reader.Read())
                        {
                            Comment comment = new Comment(reader);
                            Doing   doing   = doings.GetValue(comment.TargetID);
                            if (doing != null)
                            {
                                if (doing.CommentList.ContainsKey(comment.CommentID) == false)
                                {
                                    doing.CommentList.Add(comment);
                                }
                            }
                        }
                    }
                }
            }

            db.Connection.Close();

            //Doing doing = null;
            //int lastDoingID = -1;

            //for (int i = 0; i < comments.Count; i++)
            //{
            //    int doingID = comments[i].TargetID;

            //    if (doingID != lastDoingID)
            //    {
            //        doing = doings.GetValue(doingID);

            //        lastDoingID = doingID;
            //    }

            //    doing.CommentList.Add(comments[i]);
            //}
        }
Ejemplo n.º 2
0
        private void FillDoingComments(DoingCollection doings, SqlSession db)
        {
            if (doings.Count == 0)
                return;

            List<int> minIds = new List<int>();
            List<int> maxIds = new List<int>();

            for (int i = 0; i < doings.Count; i++)
            {
                if (doings[i].TotalComments == 0)
                    continue;
                else if (doings[i].TotalComments == 1)
                    minIds.Add(doings[i].DoingID);
                else
                {
                    minIds.Add(doings[i].DoingID);
                    maxIds.Add(doings[i].DoingID);
                }
            }

            if (minIds.Count == 0)
                return;

            using (SqlQuery query = db.CreateQuery())
            {

                query.CommandText = @"
SELECT * FROM bx_Comments WHERE CommentID IN(
    SELECT Min(CommentID) FROM [bx_Comments] WHERE [Type]=3 AND IsApproved = 1 AND [TargetID] IN(@MinTargetIDs) GROUP BY TargetID
);
";
                if (maxIds.Count > 0)
                {
                    query.CommandText += @"
SELECT * FROM bx_Comments WHERE CommentID IN(
    SELECT Max(CommentID) FROM [bx_Comments] WHERE [Type]=3 AND IsApproved = 1 AND [TargetID] IN(@MaxTargetIDs) GROUP BY TargetID
);
";
                }

                query.CreateInParameter<int>("@MinTargetIDs", minIds);
                query.CreateInParameter<int>("@MaxTargetIDs", maxIds);

                using (XSqlDataReader reader = query.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        Comment comment = new Comment(reader);
                        Doing doing = doings.GetValue(comment.TargetID);
                        if (doing != null)
                        {
                            if (doing.CommentList.ContainsKey(comment.CommentID) == false)
                                doing.CommentList.Add(comment);
                        }
                    }
                    if (reader.NextResult())
                    {
                        while (reader.Read())
                        {
                            Comment comment = new Comment(reader);
                            Doing doing = doings.GetValue(comment.TargetID);
                            if (doing != null)
                            {
                                if (doing.CommentList.ContainsKey(comment.CommentID) == false)
                                    doing.CommentList.Add(comment);
                            }
                        }
                    }
                }
            }

            db.Connection.Close();

            //Doing doing = null;
            //int lastDoingID = -1;

            //for (int i = 0; i < comments.Count; i++)
            //{
            //    int doingID = comments[i].TargetID;

            //    if (doingID != lastDoingID)
            //    {
            //        doing = doings.GetValue(doingID);

            //        lastDoingID = doingID;
            //    }

            //    doing.CommentList.Add(comments[i]);
            //}
        }