zoukankan      html  css  js  c++  java
  • DataTable转化为List

    public List<T> ConvertToList<T>(DataTable dt) where T : new()
            {
                // 定义集合
                List<T> ts = new List<T>();
                // 获得此模型的类型
                Type type = typeof(T);
                string tempName = "";
                // 获得此模型的公共属性
                PropertyInfo[] propertys = type.GetProperties();
                T t = new T();
                foreach (DataRow dr in dt.Rows)
                {
                    t = new T();
                    foreach (PropertyInfo pi in propertys)
                    {
                        tempName = pi.Name;
                        // 检查DataTable是否包含此列
                        if (dt.Columns.Contains(tempName))
                        {
                            // 判断此属性是否有Setter
                            if (!pi.CanWrite) continue;
                            if (dr[tempName] != DBNull.Value)
                                pi.SetValue(t, dr[tempName], null);
                        }
                    }
                    ts.Add(t);
                }
                return ts;
            }

    调用时:

    ConvertToList<TreeNodeInt>(ds.Tables[0])

    另附上 生成树形结构的简便方法,此处没有使用递归是他的高明之处,可以借鉴。

    public class TreeNodeInt
        {
            public int id { get; set; }
            public int? parentId { get; set; }
            public string title { get; set; }
            public string key { get; set; }
            public string longLabel { get; set; }
            public bool isFolder { get; set; }
            public bool isLazy { get; set; }
            public bool expand { get; set; }
            //public bool Select { get; set; }
            public bool unselectable { get; set; }
            public bool hideCheckbox { get; set; }
            public int treeLevel { get; set; }
            public object obj { get; set; }
            public List<TreeNodeInt> children { get; set; }

            public static List<TreeNodeInt> ConvertToTree(List<TreeNodeInt> itemList)
            {
                itemList = itemList.OrderBy(p => p.title).ToList();
                var tree = (from i in itemList
                            where i.parentId == null || i.parentId == 0
                            //orderby i.title
                            select new TreeNodeInt
                            {
                                id = i.id,
                                key = i.key,
                                //key = i.id.ToString(),
                                title = i.title,
                                longLabel = i.longLabel,
                                isFolder = i.isFolder,
                                expand = i.expand,
                                isLazy = i.isLazy,
                                //Select = i.Select,
                                unselectable = i.unselectable,
                                hideCheckbox = i.hideCheckbox,
                                treeLevel = i.treeLevel,
                                obj = i.obj
                            }).ToList();
                Queue<TreeNodeInt> queue = new Queue<TreeNodeInt>(tree);

                while (queue.Count > 0)
                {
                    var node = queue.Dequeue();
                    var children = (from i in itemList
                                    where i.parentId == node.id && i.id != node.id
                                    //let idStr = SqlFunctions.StringConvert((double)i.id).Trim()
                                    select new TreeNodeInt
                                    {
                                        id = i.id,
                                        parentId = i.parentId,
                                        key = i.key,
                                        //key = i.id.ToString(),
                                        title = i.title,
                                        longLabel = i.longLabel,
                                        isFolder = i.isFolder,
                                        expand = i.expand,
                                        isLazy = i.isLazy,
                                        //Select = i.Select,
                                        unselectable = i.unselectable,
                                        hideCheckbox = i.hideCheckbox,
                                        treeLevel = i.treeLevel,
                                        obj = i.obj
                                    }).ToList();
                    if (children.Count > 0)
                    {
                        node.children = children;
                        foreach (var child in children)
                        {
                            queue.Enqueue(child);
                        }
                    }
                }

                return tree;
            }
        }

  • 相关阅读:
    C++ 多线程 (4) 互斥量(mutex)与锁(lock)
    C++ 多线程(3)std::thread 详解
    c++ 多线程(2)创建线程对象的方法
    CMake解决c++11的phread库问题:undefined reference to `pthread_create’
    生成对抗网络--Generative Adversarial Networks (GAN)
    语义分割(semantic segmentation)——U-Net
    目标检测SSD: Single Shot MultiBox Detector
    基于内容的图像检索(CBIR) ——以图搜图
    去噪自动编码器
    利用Chrome开发者工具功能进行网页整页截图的方法
  • 原文地址:https://www.cnblogs.com/itjeff/p/5355713.html
Copyright © 2011-2022 走看看