国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

csharp:Conversion Between DataTable and List

2019-11-17 02:18:55
字體:
來源:轉載
供稿:網友

csharp:Conversion Between DataTable and List

/// <summary>    /// http://www.codePRoject.com/Tips/784090/Conversion-Between-DataTable-and-List-in-Csharp     /// </summary>    public static class ExtensionUtility    {            /// <summary>        /// Converts List To DataTable        /// </summary>        /// <typeparam name="TSource"></typeparam>        /// <param name="data"></param>        /// <returns></returns>        public static DataTable ToDataTable<TSource>(this IList<TSource> data)        {            DataTable dataTable = new DataTable(typeof(TSource).Name);            PropertyInfo[] props = typeof(TSource).GetProperties(BindingFlags.Public | BindingFlags.Instance);            foreach (PropertyInfo prop in props)            {                dataTable.Columns.Add(prop.Name, Nullable.GetUnderlyingType(prop.PropertyType) ?? prop.PropertyType);            }            foreach (TSource item in data)            {                var values = new object[props.Length];                for (int i = 0; i < props.Length; i++)                {                    values[i] = props[i].GetValue(item, null);                }                dataTable.Rows.Add(values);            }            return dataTable;        }              /// <summary>        /// Converts DataTable To List        /// </summary>        /// <typeparam name="TSource"></typeparam>        /// <param name="dataTable"></param>        /// <returns></returns>        public static List<TSource> ToList<TSource>(this DataTable dataTable) where TSource : new()        {            var dataList = new List<TSource>();            const BindingFlags flags = BindingFlags.Public | BindingFlags.Instance | BindingFlags.NonPublic;            var objFieldNames = (from PropertyInfo aProp in typeof(TSource).GetProperties(flags)                                 select new { Name = aProp.Name, Type = Nullable.GetUnderlyingType(aProp.PropertyType) ?? aProp.PropertyType }).ToList();            var dataTblFieldNames = (from DataColumn aHeader in dataTable.Columns                                     select new { Name = aHeader.ColumnName, Type = aHeader.DataType }).ToList();            var commonFields = objFieldNames.Intersect(dataTblFieldNames).ToList();            foreach (DataRow dataRow in dataTable.AsEnumerable().ToList())            {                var aTSource = new TSource();                foreach (var aField in commonFields)                {                    PropertyInfo propertyInfos = aTSource.GetType().GetProperty(aField.Name);                    var value = (dataRow[aField.Name] == DBNull.Value) ? null : dataRow[aField.Name]; //if database field is nullable                    propertyInfos.SetValue(aTSource, value, null);                }                dataList.Add(aTSource);            }            return dataList;        }    }

  測試數據源:

public class Student    {        public long Id { get; set; }        public string Name { get; set; }        public short Age { get; set; }        public DateTime DateOfCreation { get; set; }        public bool? IsActive { get; set; }    }    public class Teacher    {        public long Id { get; set; }        public string Name { get; set; }        public Nullable<int> DepartmentId { get; set; }    }    public class Data    {        public static List<Student> GetStudents()        {            var list = new List<Student>            {                new Student {Id = 1, Name = "Smith", Age = 18, DateOfCreation = DateTime.Now, IsActive = true},                new Student {Id = 2, Name = "Hook", Age = 16, DateOfCreation = DateTime.Now.AddDays(-1), IsActive = true},                new Student {Id = 3, Name = "Jhon", Age = 15, DateOfCreation = DateTime.Now.AddDays(-2), IsActive = true},                new Student {Id = 4, Name = "Alan", Age = 21, DateOfCreation = DateTime.Now.AddDays(-3), IsActive = true}            };            return list;        }        public static List<Teacher> GetTeachers()        {            var list = new List<Teacher>            {                new Teacher {Id = 1, Name = "Smith", DepartmentId = 18 },                new Teacher {Id = 2, Name = "Hook", DepartmentId = 16 },                new Teacher {Id = 3, Name = "Jhon", DepartmentId = 15 },                new Teacher {Id = 4, Name = "Alan", DepartmentId = 21 }            };            return list;        }        public static DataTable DbNullInt()        {            DataTable table = new DataTable();            table.Columns.Add("Id", typeof(long));            table.Columns.Add("Name", typeof(string));            DataColumn column;            column = new DataColumn("DepartmentId", System.Type.GetType("System.Int32"));            column.AllowDBNull = true;            table.Columns.Add(column);            table.Rows.Add(1, "Smith", DBNull.Value);            table.Rows.Add(2, "Hook", 1);            return table;        }    }

  測試:

            List<Student> students = Data.GetStudents();            /*List to DataTable conversion*/            DataTable studentTbl = students.ToDataTable();            /*DataTable to List conversion*/            List<Student> newStudents = studentTbl.ToList<Student>();//ExtensionUtility.ToList<Student>(newStudents);            this.dataGridView1.DataSource = newStudents;            /*List to DataTable conversion*/            DataTable teacherTbl = Data.DbNullInt();            /*DataTable to List conversion*/            List<Teacher> newTeachers = teacherTbl.ToList<Teacher>();                       this.dataGridView2.DataSource = newTeachers;

  


上一篇:通俗版說委托

下一篇:C#串口控制

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 房产| 区。| 常宁市| 芒康县| 明溪县| 邯郸县| 丘北县| 五寨县| 顺昌县| 平顶山市| 通渭县| 栾城县| 延寿县| 大洼县| 临汾市| 日照市| 凌海市| 沈阳市| 禄劝| 双桥区| 石城县| 上杭县| 夹江县| 伊宁市| 牡丹江市| 江源县| 台东县| 永川市| 屏东县| 淮阳县| 广河县| 台中县| 浑源县| 威宁| 黑龙江省| 翁牛特旗| 盐亭县| 手机| 徐水县| 宝丰县| 九寨沟县|