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;
            }
       
        }
  • 相关阅读:
    Linux下面编译安装ffmpeg
    Fidder简单使用方法(HTTPS抓取和url替换)
    关于一下个阶段的计划
    JAVA的随机的字符串的封装(基本上够用了)
    Shell Script中的间接变量引用
    进程概念
    int main(int argc, char *argv[])的解读
    存储数组数据到SharedPreferences
    C语言中的基本声明
    C中关于指针数组的用法
  • 原文地址:https://www.cnblogs.com/yellowcool/p/8297930.html
Copyright © 2011-2022 走看看