• List转换为Datatable


    static class Extensions
        {
            internal static DataSet ToDataSet<T>(this IList<T> list)
            {
                Type elementType = typeof(T);
                var ds = new DataSet();
                var t = new DataTable();
                ds.Tables.Add(t);
                elementType.GetProperties().ToList().ForEach(propInfo => t.Columns.Add(propInfo.Name, Nullable.GetUnderlyingType(propInfo.PropertyType) ?? propInfo.PropertyType));
                foreach (T item in list)
                {
                    var row = t.NewRow();
                    elementType.GetProperties().ToList().ForEach(propInfo => row[propInfo.Name] = propInfo.GetValue(item, null) ?? DBNull.Value);
                    t.Rows.Add(row);
                }
                return ds;
            }
        }
    View Code

    调用上面的方法即可进行转换

    例:

    var dsData = List数据源.ToDataSet();

    完整代码:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Data;
    namespace WebApplication2
    {
        public partial class _Default : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    var list = new List<Demo> {
                        new Demo{ id=1,age=18, name="Tim"},
                        new Demo{ id=2,age=22, name="Allen"},
                        new Demo{ id=3,age=24, name="Jim"}
                    };
                    var ds = list.ToDataSet();
                    GridView1.DataSource = ds.Tables[0];
                    GridView1.DataBind();
                }
            }
        }
    
        static class Extensions
        {
            internal static DataSet ToDataSet<T>(this IList<T> list)
            {
                Type elementType = typeof(T);
                var ds = new DataSet();
                var t = new DataTable();
                ds.Tables.Add(t);
                elementType.GetProperties().ToList().ForEach(propInfo => t.Columns.Add(propInfo.Name, Nullable.GetUnderlyingType(propInfo.PropertyType) ?? propInfo.PropertyType));
                foreach (T item in list)
                {
                    var row = t.NewRow();
                    elementType.GetProperties().ToList().ForEach(propInfo => row[propInfo.Name] = propInfo.GetValue(item, null) ?? DBNull.Value);
                    t.Rows.Add(row);
                } 
                return ds;
            }
        }
        class Demo
        {
            public int id { get; set; }
            public string name { get; set; }
            public int age { get; set; }
        }
    }
    View Code
  • 相关阅读:
    2012-2013年度大总结
    [每日一题] 11gOCP 1z0-052 :2013-08-31 数据库的存储结构....................................................A8
    Oracle约束操作
    几道字典树题目
    1032
    uva 10718 Bit Mask(贪心)
    找工作笔试面试那些事儿(2)---内存管理那些事
    汉语-词语:结构
    汉语-词语:形式
    汉语-词语:方向
  • 原文地址:https://www.cnblogs.com/SmilePastaLi/p/6860383.html
Copyright © 2020-2023  润新知