سؤال

أقوم بإنشاء نظام تعليقات مترابطة لموقع ويب خاص بي وواجهت مشكلة...

لدي قائمة تم سحبها من قاعدة بيانات تحتوي على حقل معرف وحقل معرف الوالدين.يمكن أن يكون حقل المعرف الأصلي خاليًا، لكن حقل المعرف لن يكون فارغًا أبدًا.

نظرًا لأن هذا سيكون نظام تعليق مترابط، فأنا أقوم بتنظيم القائمة حيث يكون المعرف هو الأعلى، ولكن إذا كان المعرف الأصلي موجودًا، فسيتم إدراجه تحت المعرف.ثم يمكن أن يستمر هذا إلى ما لا نهاية أيضًا.إذن، يحتوي المستوى الثاني الآن أيضًا على معرف وأريد إدراج أي عنصر بمعرف أصل لهذا المعرف تحته.

على سبيل المثال:

---1.بلاه

--------2.بلاه بلاه -> معرف الوالدين = 1

-----------3.بلاه بلاه -> معرف الوالدين = 2

-------------- 4.بلاه بلاه ->معرف الوالدين = 3

----------- 3. بلاه بلاه -> معرف الوالدين = 2

--------2.بلاه بلاه -> معرف الوالدين = 1

أعتقد أنك تفهم ما أعنيه.

إذن هذا ما لدي حتى الآن ...

List<comment> finalList = new List<comment>();
    for (int i = 0; i < getComments.Count(); i++)
    {
        string item = getComments[i].parentComment;
        getComments[i].threadID = 1;
        finalList.Add(getComments[i]);
        for (int ii = 0; ii < getComments.Count(); ii++)
        {
            if (getComments[ii].commentID == item)
            {
                getComments[ii].threadID = 2;
                finalList.Add(getComments[i]);
            }
        }
    }

يبدو أن الأمر قد تم حله في منتصف الطريق، ولكن ليس حقًا ...إن ThreadID هو بالطبع مدى زراعته على اليمين.

هل كانت مفيدة؟

المحلول 2

والشكر للجميع الرجال مساعدتكم. أنا لا نقدر عليه.

وأنا لم الرغم من العثور على شيء من قبل الرجل الذي كتب كل شيء على الاطلاق لذلك.

http://www.scip.be/index.php؟Page=ArticlesNET23

http://www.scip.be/index.php؟Page=ArticlesNET09

http://www.scip.be/index.php؟Page=ArticlesNET18

نصائح أخرى

نظرًا لأنك تستخدم طريقة الامتداد Count() بدلاً من خاصية Count (والتي تعتبر عدم كفاءة طفيفة في حد ذاتها؛من الأفضل أن تبدأ باستخدام foreach) من المفترض أنك تستخدم .NET 3.5.

لا أعتقد أنني أفهم مخططك تمامًا - على سبيل المثال، ما الذي يمكن قوله أن التعليق باستخدام معرف الخيط = 4 في الرسم التخطيطي الخاص بك يقع ضمن العنصر الأول لمعرف الخيط = 3 بدلاً من العنصر الثاني؟

دون معرفة الكثير من تفاصيل ما تبحث عنه، بشكل عام سأفكر في بنية بيانات التعليق باستخدام:

  • معرف التعليق:معرف هذا الكيان
  • معرف الجذر:معرف العنصر الجذر للسلسلة (حتى تتمكن من جلب جميع التعليقات للسلسلة بسهولة)
  • معرف الوالدين:معرف التعليق الخاص بالأصل لهذا التعليق، أو فارغ إذا كان العنصر الجذر
  • الطابع الزمني:أو أي شيء آخر من شأنه أن يسمح بفرز تعليقات الطفل داخل أحد الوالدين بشكل مناسب.

نظرًا لذلك، سيكون من السهل جدًا تحديد مستوى المسافة البادئة، إذا كان هذا هو ما يقلقك.إذا كان ذلك يبدو مفيدًا، فيمكنني الخوض في مزيد من التفاصيل - وإذا لم يكن الأمر كذلك، فيرجى توضيح السؤال.

وكنت في حاجة الى وظيفة متكررة، وعلى أساس كيف يبدو وكأنك تعبر القائمة، فإنه ربما يكون من الأفضل لتخزين ID وChildID (بدلا من ID الأم). بهذه الطريقة وظيفة متكررة يمكن أن ينتهي في اجتياز عندما ChildID == فارغة.

وهذا قد عمل:

class Program
    {
        static void Main(string[] args)
        {
            CommentCollection collection=new CommentCollection();
            Comment c1=new Comment("Blah",1,0,collection);
            Comment c2=new Comment("Blah blah",2,1,collection);
            Comment c3=new Comment("Blah blah", 3, 2, collection);
            Console.WriteLine(collection);
        }
    }
    [DebuggerDisplay("{id}-{parentId}: {text}")]
    class Comment:IEnumerable<Comment>
    {
        private readonly CommentCollection collection;
        private readonly int parentId;

        public Comment(string text, int id, int parentId, CommentCollection collection)
        {
            Id = id;
            this.parentId = parentId;
            collection.Add(this);
            this.collection = collection;
            this.text = text;
        }
        public Comment Parent
        {
            get
            {
                if (parent == null)
                {
                    parent = parentId == 0 ? null : collection[parentId];
                }
                return parent;
            }
        }

        private Comment parent;
        private readonly string text;
        public int Id{ get; private set;}
        public IEnumerator<Comment> GetEnumerator()
        {
            return collection.Where(c => c.Parent == this).GetEnumerator();
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }
        public int Level
        {
            get { return Parent == null ? 0 : Parent.Level + 1; }
        }
        public override string ToString()
        {
            return Parent == null ? text : Parent + " > " + text;
        }
        public string ToString(bool tree)
        {
            if (!tree)
            {
                return ToString();
            }
            else
            {
                StringBuilder output = new StringBuilder();
                output.AppendLine(new string(' ', Level) + ToString(false));
                foreach (Comment comment in this)
                {
                    output.AppendLine(comment.ToString(true));
                }
                return output.ToString();
            }
        }
    }
    class CommentCollection:IEnumerable<Comment>
    {
        public void Add(Comment comment)
        {
            comments.Add(comment.Id,comment);
        }
        public Comment this[int id]
        {
            get { return comments[id]; }
        }
        private readonly Dictionary<int,Comment> comments=new Dictionary<int, Comment>();

        public IEnumerator<Comment> GetEnumerator()
        {
            return comments.Select(p => p.Value).GetEnumerator();
        }

        public IEnumerable<Comment> GetTopLevel()
        {
            return comments.Where(c => c.Value.Parent == null).
                Select(c => c.Value);
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }
        public override string ToString()
        {
            StringBuilder output=new StringBuilder();
            foreach (Comment comment in GetTopLevel())
            {
                output.AppendLine(comment.ToString(true));
            }
            return output.ToString();
        }
    }
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top