zoukankan      html  css  js  c++  java
  • datatable填装List代替for循环

    public class DataToModelHelper<T> where T : new()
        {
            public static IList<T> ConvertToModel(DataTable dt)
            {
                //定义集合
                IList<T> ts = new List<T>();
                T t = new T();
                string tempName = "";
                //获取此模型的公共属性
                PropertyInfo[] propertys = t.GetType().GetProperties();
                foreach (DataRow row in dt.Rows)
                {
                    t = new T();
                    foreach (PropertyInfo pi in propertys)
                    {
                        tempName = pi.Name;
                        //检查DataTable是否包含此列
                        if (dt.Columns.Contains(tempName))
                        {
                            //判断此属性是否有set
                            if (!pi.CanWrite)
                                continue;
                            object value = row[tempName];
                            if (value != DBNull.Value)
                            {
                                if (pi.PropertyType == typeof(string))
                                {
                                    pi.SetValue(t, value.ToString(), null);
                                }
                                else if (pi.PropertyType == typeof(int) || pi.PropertyType == typeof(int?))
                                {
                                    pi.SetValue(t, int.Parse(value.ToString()), null);
                                }
                                else if (pi.PropertyType == typeof(DateTime?) || pi.PropertyType == typeof(DateTime))
                                {
                                    pi.SetValue(t, DateTime.Parse(value.ToString()), null);
                                }
                                else if (pi.PropertyType == typeof(float))
                                {
                                    pi.SetValue(t, float.Parse(value.ToString()), null);
                                }
                                else if (pi.PropertyType == typeof(double))
                                {
                                    pi.SetValue(t, double.Parse(value.ToString()), null);
                                }
                                else if (pi.PropertyType == typeof(decimal))
                                {
                                    pi.SetValue(t, decimal.Parse(value.ToString()), null);
                                }
                                else if (pi.PropertyType == typeof(Int64))
                                {
                                    pi.SetValue(t, long.Parse(value.ToString()), null);
                                }
                                else
                                {
                                    pi.SetValue(t, value, null);
                                }
                            }
                        }                   
                    }
                    ts.Add(t);               
                }
                return ts;
            }
       
        }
  • 相关阅读:
    o1-check-power-of-2 && search-a-2d-matrix
    Remove Linked List Elements &&remove-duplicates-from-sorted-list
    fibonacci && climbing-stairs
    2016 Multi-University Training Contest 1 Abandoned country
    中断调用与子程序设计实验
    POJ 3764
    CSU 1566 The Maze Makers
    poj2524 Ubiquitous Religions
    poj2377
    汇编语言程序设计的上机基础知识预备
  • 原文地址:https://www.cnblogs.com/yellowcool/p/8297930.html
Copyright © 2011-2022 走看看