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. base 64 编解码器

    base 64 编解码 1. base64的编码都是按字符串长度,以每3个8bit的字符为一组, 2. 然后针对每组.首先获取每一个字符的ASCII编码. 3. 然后将ASCII编码转换成8bit的二 ...

  2. C#中的预编译指令介绍

    原文:C#中的预编译指令介绍 1.#define和#undef 用法: #define DEBUG #undef DEBUG #define告诉编译器,我定义了一个DEBUG的一个符号,他类似一个变量 ...

  3. thinkphp达到UploadFile.class.php图片上传功能

    片上传在站点里是非经常常使用的功能.ThinkPHP里也有自带的图片上传类(UploadFile.class.php) 和图片模型类(Image.class.php).方便于我们去实现图片上传功能,以 ...

  4. Tuple

    Tuple(组元)是C# 4.0引入的一个新特性,编写的时候需要基于.NET Framework 4.0或者更高版本. 在以前编程中,当需要返回多个值得方法中,常常需要将这些值放置到一个结构体或者对象 ...

  5. Attribute(两)——定义自己的特色+Asp.net MVC中间filter详细解释

    部分博客是预先定义的有关特性的一些基本特征,同时还Attribute这一概念的一个宏观上的认识,在上篇博客结尾介绍了有关为自己定义特性服务的AttributeUsage,这篇博客主要是通过filter ...

  6. rabbitmq的java简单的实现

    1,安装rabbitmq.我的是ubuntu14.04,在官网上面下载最新的安装文件http://www.rabbitmq.com/install-debian.html 2.安装完之后  启动rab ...

  7. MPC8313ERDB不新鲜pkg包裹,把文件放进Ramdisk

    MPC8313ERDB不新鲜pkg包裹,把文件放进Ramdisk 经ltib编译器生成rootfs.ext2.gz.uboot它可以直接uboot采用.假设我们编写了相应的外部文件把Ramdisk往里 ...

  8. 移动端 (基于jquery的3个)touch插件

    //第一个 Author: Alone Antroduction: 高级前端开发工程师 Sign: 人生没有失败,只有没到的成功. //依赖jQuery 或者Zepto <script> ...

  9. String.Format in Java and C#

    原文:String.Format in Java and C# JDK1.5中,String类新增了一个很有用的静态方法String.format(): format(Locale l, String ...

  10. AndroidManifest:VersionCode和VersionName

    Google为APK定义了两个关于版本号属性:VersionCode和VersionName,他们有不同的用途. VersionCode:对消费者不可见.仅用于应用市场.程序内部识别版本号,推断新旧等 ...