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;
        }
    }

DataTable转化为List的更多相关文章

  1. C#将DataTable转化为List<T>

    C#将DataTable转化为List<T> 在使用三层架构开发一个网站时,希望把DataTable对象转换为List<T>对象,于是在网上找资料,总结一个比较方便的方法来实现 ...

  2. LINQ查询返回DataTable类型[轉]與将DataTable序列化为Json格式【轉】

    (原文地址:http://xuzhihong1987.blog.163.com/blog/static/26731587201101853740294/) LINQ查询返回DataTable类型 在使 ...

  3. C#中DataTable转化为List<T>解析

    在.net项目中使用到DataTable和List<T>集合的地方较多, 泛型的好处: 它为使用c#语言编写面向对象程序增加了极大的效力和灵活性.不会强行对值类型进行装箱和拆箱,或对引用类 ...

  4. DataTable转化为Model

    /// <summary> /// 将DataTable转成Model /// </summary> /// <param name="dt"> ...

  5. 将DataTable格式化为json字符串返回

    一般用于ajax局部刷新的使用比较多,通过查询得到了DataTable数据,要想将数据放回需要将DataTable转换为json格式,以下为转换的调用函数: string json = "& ...

  6. .NET DataTable转化为json格式

    标准的json用“分隔,不用' public static string DataSetToJson(DataTable dt) {        string json = string.Empty ...

  7. 将DataTable转化为json对象

    private string DataTableTojson(DataTable dt)         {              List> list=new List>();    ...

  8. 将SqlDataReader 数据集转化为datatbale ,在将datatable 转化为iList

    public IList GetModelList(string tablename, string where) { IList list = null; DataTable dataTable = ...

  9. DataTable 转换成 Json的3种方法

    在web开发中,我们可能会有这样的需求,为了便于前台的JS的处理,我们需要将查询出的数据源格式比如:List<T>.DataTable转换为Json格式.特别在使用Extjs框架的时候,A ...

随机推荐

  1. Property &#39;sqlSessionFactory&#39; or &#39;sqlSessionTemplate&#39; are required

    之前一直用mybatis+mybatis-spring-1.1.1,系统升级mybatis使用后 mybatis-spring-1.2.2, 再其他配置均为改动的情况下执行出错: Property ' ...

  2. 【转】c++继承:公有、私有、保护

    原文链接:http://www.cnblogs.com/qlwy/archive/2011/08/25/2153584.html 公有继承(public).私有继承(private).保护继承(pro ...

  3. unity3d中让物体显示和隐藏

    unity3d中让物体显示和隐藏的方法 gameObject.renderer.enabled //是控制一个物体是否在屏幕上渲染或显示  而物体实际还是存在的 仅仅是想当于隐身 而物体本身的碰撞体还 ...

  4. eclipse在maven项目交付svn忽略简介

    文章来源:http://blog.csdn.net/chaijunkun/article/details/34805385,转载请注明. 不时因为它将有关鲍恩梳理,它会做出相应的内容不变.文. ecl ...

  5. CacheManager

    .Net缓存管理框架CacheManager Cache缓存在计算机领域是一个被普遍使用的概念.硬件中CPU有一级缓存,二级缓存, 浏览器中有缓存,软件开发中也有分布式缓存memcache, redi ...

  6. CentOS-6.5-x86_64 最小化安装后,怎样安装 man 程序?

    CentOS-6.5-x86_64 最小化安装后.怎样安装man 程序? CentOS-6.5-x86_64 最小化安装后,没有man 程序,没它还真的不方便. man 是 manual(手冊)的意思 ...

  7. [Aaronyang] 写给自己的WPF4.5 笔记15[AyArc诞生-WPF版本绚丽的环状图,Ay制作,AyWindow强势预览]

    原文:[Aaronyang] 写给自己的WPF4.5 笔记15[AyArc诞生-WPF版本绚丽的环状图,Ay制作,AyWindow强势预览]  我的文章一定要做到对读者负责,否则就是失败的文章  -- ...

  8. hdu 4059 The Boss on Mars(纳入和排除)

    http://acm.hdu.edu.cn/showproblem.php?pid=4059 定义S = 1^4 + 2^4 + 3^4+.....+n^4.如今减去与n互质的数的4次方.问共降低了多 ...

  9. Java拾遗(一):浅析Java子类和父类的实例化顺序 及 陷阱

    本文主要介绍Java里经常使用的子类和父类的变量实例化顺序及陷阱,并结合一个Android实例来探讨此问题.日后编程中应尽量避免此陷阱. 首先看以下一段代码: 定义一个虚类Server.java pa ...

  10. 【Leetcode】Partition List (Swap)

    Given a linked list and a value x, partition it such that all nodes less than x come before nodes gr ...