zoukankan      html  css  js  c++  java
  • C# DataTable转实体+實例

    一、datatable轉實體代碼


     /// <summary>

            /// DataTable转实体
            /// </summary>
            /// <typeparam name="T"></typeparam>
            /// <param name="dt"></param>
            /// <returns></returns>
            public List<T> DataTableConvertEntity<T>(DataTable dt) where T : class, new()
            {
                if (dt == null || dt.Rows.Count == 0)
                {
                    return null;
                }
                List<T> List = new List<T>();
                foreach (DataRow dr in dt.Rows)
                {
                    T model = new T();
                    for (int i = 0; i < dr.Table.Columns.Count; i++)
                    {
                        PropertyInfo propertyInfo = model.GetType().GetProperty(dr.Table.Columns[i].ColumnName);
                        if (propertyInfo != null && dr[i] != DBNull.Value)
                            propertyInfo.SetValue(model, dr[i], null);
                    }

                    List.Add(model);
                }
                return List;
            }


     /// <summary>

            /// DataTable转实体+判斷空值---此方法可以GET到欄位名和值
            /// </summary>
            /// <typeparam name="T"></typeparam>
            /// <param name="dt"></param>
            /// <returns></returns>

    public static List<T> ConvertToModel<T>(DataTable dt) where T : new()

    {
    // 定义集合
    List<T> ts = new List<T>();

    // 获得此模型的类型
    Type type = typeof(T);
    string tempName = "";

    foreach (DataRow dr in dt.Rows)
    {
    T t = new T();
    // 获得此模型的公共属性
    PropertyInfo[] propertys = t.GetType().GetProperties();
    foreach (PropertyInfo pi in propertys)
    {
    tempName = pi.Name; // 检查DataTable是否包含此列

    if (dt.Columns.Contains(tempName))
    {
    // 判断此属性是否有Setter
    if (!pi.CanWrite) continue;

    object value = dr[tempName];
    if (value == DBNull.Value) continue;
    if (pi.PropertyType == typeof(string))
    {
    pi.SetValue(t, value.ToString(), null);
    }
    else
    {
    pi.SetValue(t, value, null);
    }
    }
    }
    ts.Add(t);
    }
    return ts;
    }

    public static List<T> ToList<T>(this DataTable dt) where T : class, new()
    {

    var list =ConvertToModel<T>(dt);
    return list;
    }


    二、使用案例

    public List<MSDS> GetChemicalNameAndChemicalArea(string MappingType, string adminEmpno)
    {
    DataTable dt = new DataTable();
    #region 取得mapping表 站點信息
    Msds_Mapping value = new Msds_Mapping()
    {
    mapping_type = MappingType,
    value1 = "0",
    value2 = "0",
    value3 = "0",
    value4 = "0",
    value5 = "0"
    };
    dt = _msdsDB.Mapping(adminEmpno, "admin", value);//取得mapping表
    //List<Msds_Mapping> list = DataTableConvertEntity<Msds_Mapping>(dt);//DataTable转实体

    List<Msds_Mapping> list = dt.ToList<Msds_Mapping>();

    List<MSDS> list1 = new List<MSDS>();
    foreach (var item in list)
    {
    MSDS MSDS = new MSDS()
    {
    label=item.value1,
    value=item.value2
    };
    list1.Add(MSDS);
    }
    #endregion
    return list1;
    }

  • 相关阅读:
    Kubernetes之network: failed to set bridge addr: "cni0" already has an IP address different from xxx问题
    k8s的存储Volume
    系统漏洞扫描与分析软件
    linux图形化安装oracle
    JMX监控tomcat jdbc pool
    Hyper-V
    苹果手机
    读书
    clickhouse count
    clickhouse分布式表
  • 原文地址:https://www.cnblogs.com/popo1/p/13453837.html
Copyright © 2011-2022 走看看