Example #1
0
        /// <summary>
        /// 将指定树型对象集合转成标准树形集合
        /// </summary>
        /// <param name="items"></param>
        /// <param name="ChildPropertys"></param>
        /// <returns></returns>
        public static TreeEntityList ToTreeEntity(this IEnumerable items, string ChildPropertys)
        {
            TreeEntityList nodes = new TreeEntityList();

            foreach (object o in items)
            {
                TreeContainer node   = new TreeContainer(o);
                IEnumerable   cnodes = GetTreeEntityChilds(o, ChildPropertys);
                if (cnodes != null)
                {
                    foreach (TreeContainer tc in cnodes)
                    {
                        node.Childs.Add(tc);
                    }
                }
                nodes.Add(node);
            }
            return(nodes);
        }
Example #2
0
        /// <summary>
        /// 根据属性的关联将指定对象集封装成标准树型结构
        /// </summary>
        /// <param name="items"></param>
        /// <param name="ParentIDProperty"></param>
        /// <param name="KeyProperty"></param>
        /// <returns></returns>
        public static TreeEntityList ToTreeEntity(this IEnumerable items, string ParentIDProperty, string KeyProperty)
        {
            Dictionary <object, TreeContainer> tmpList = new Dictionary <object, TreeContainer>();
            TreeEntityList nodes = new TreeEntityList();
            Type           type  = null;
            int            count = 0;

            if (ParentIDProperty.IsWhiteSpace() || KeyProperty.IsWhiteSpace())
            {
                foreach (object o in items)
                {
                    nodes.Add(new TreeContainer(o));
                }
            }
            else
            {
                foreach (object o in items)
                {
                    count++;
                    if (o == null)
                    {
                        continue;
                    }
                    TreeContainer tc;
                    if (tmpList.ContainsKey(o))
                    {
                        tc = tmpList[o];
                    }
                    else
                    {
                        tc         = new TreeContainer(o);
                        tmpList[o] = tc;
                    }

                    if (type == null)
                    {
                        type = o.GetType();
                    }
                    object pv = GetPropertyValue(o, ParentIDProperty, type);
                    //为顶层(没有上一级,则视为顶层)
                    if (pv.IsNullOrDBNull() || pv.ToStringValue().IsWhiteSpace())
                    {
                        nodes.Add(tc);
                    }

                    //查找其子集
                    object kv = GetPropertyValue(o, KeyProperty, type);
                    if (kv == null) //当前项无值,则作为顶层
                    {
                        if (!nodes.Contains(tc))
                        {
                            nodes.Add(tc);
                        }
                        continue;
                    }
                    var s = from item in items.Cast <object>()
                            where (item != o && kv.Equals(GetPropertyValue(item, ParentIDProperty, type)))
                            select item;

                    foreach (object obj in s)
                    {
                        TreeContainer ctc;
                        if (tmpList.ContainsKey(obj))
                        {
                            ctc = tmpList[obj];
                        }
                        else
                        {
                            ctc          = new TreeContainer(obj);
                            tmpList[obj] = ctc;
                        }
                        if (!tc.Childs.Contains(ctc))
                        {
                            ctc.Parent = tc;
                            tc.Childs.Add(ctc);
                            if (nodes.Contains(ctc))
                            {
                                nodes.Remove(ctc);
                            }
                        }
                    }
                    if (tc.Parent == null && !nodes.Contains(tc))
                    {
                        nodes.Add(tc);
                    }
                }
            }
            return(nodes);
        }
Example #3
0
        /// <summary>
        /// 根据指定的XPath对象集转成标准树型集
        /// </summary>
        /// <param name="items"></param>
        /// <param name="XPathProperty"></param>
        /// <param name="IsSameLenth"></param>
        /// <param name="XPathLength"></param>
        /// <returns></returns>
        public static TreeEntityList ToTreeEntity(this IEnumerable items, string XPathProperty, bool IsSameLenth, int XPathLength, int PrefixLength, int LstfixLength)
        {
            Dictionary <object, TreeContainer> tmpList = new Dictionary <object, TreeContainer>();
            TreeEntityList nodes = new TreeEntityList();
            Type           type  = null;
            int            count = 0;

            if (XPathProperty.IsWhiteSpace())
            {
                foreach (object o in items)
                {
                    nodes.Add(new TreeContainer(o));
                }
            }
            else
            {
                foreach (object o in items)
                {
                    count++;
                    if (o == null)
                    {
                        continue;
                    }
                    TreeContainer tc;
                    if (tmpList.ContainsKey(o))
                    {
                        tc = tmpList[o];
                    }
                    else
                    {
                        tc         = new TreeContainer(o);
                        tmpList[o] = tc;
                    }

                    if (type == null)
                    {
                        type = o.GetType();
                    }
                    string xpath = GetPropertyValue(o, XPathProperty, type).ToStringValue();               //获取该层的XPath值
                    //为顶层
                    if (xpath.IsWhiteSpace() || xpath.Length == XPathLength + PrefixLength + LstfixLength) //顶层
                    {
                        nodes.Add(tc);
                    }
                    //查找其子集
                    var s = from item in items.Cast <object>()
                            where IsChild(o, item, XPathProperty, type, XPathLength, PrefixLength, LstfixLength, xpath, IsSameLenth)
                            select item;

                    foreach (object obj in s)
                    {
                        TreeContainer ctc;
                        if (tmpList.ContainsKey(obj))
                        {
                            ctc = tmpList[obj];
                        }
                        else
                        {
                            ctc          = new TreeContainer(obj);
                            tmpList[obj] = ctc;
                        }
                        if (!tc.Childs.Contains(ctc))
                        {
                            tc.Childs.Add(ctc);
                        }
                    }
                }
            }
            return(nodes);
        }