如何处理null-able类型将datatable转换为list [英] How to handle null-able type convert datatable to list

查看:142
本文介绍了如何处理null-able类型将datatable转换为list的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何处理null-able类型将数据表转换为列表



How to handle null-able type convert datatable to list

public static class DataTableToList
   {
       public static List<T> ToList<T>(this DataTable table) where T : class, new()
       {
           try
           {
               List<T> list = new List<T>();

               foreach (var row in table.AsEnumerable())
               {
                   T obj = new T();

                   foreach (var prop in obj.GetType().GetProperties())
                   {
                       try
                       {
                           PropertyInfo propertyInfo = obj.GetType().GetProperty(prop.Name);
                           propertyInfo.SetValue(obj, Convert.ChangeType(row[prop.Name], propertyInfo.PropertyType), null);
                       }
                       catch
                       {
                           continue;
                       }
                   }

                   list.Add(obj);
               }

               return list;
           }
           catch
           {
               return null;
           }
       }
   }





我尝试过:



如何处理null-able类型将datatable转换为list



What I have tried:

How to handle null-able type convert datatable to list

推荐答案

所有你需要做的是在你现有的代码中,添加一行来检查数据行值是否为null,以及< T>的属性是否为空。也可以为空:



All you need to do is in your existing code, add a line to check if the data row value is null and also if the property of <T> is nullable too:

if(row[prop.Name] == DBNull.Value /* or == null */
   && prop.PropertyType.IsValueType
   && Nullable.GetUnderlyingType(prop.PropertyType) != null)
   // if property is nullable, underlying nullable type will eval as true.
{
    propertyInfo.SetValue(obj, Convert.ChangeType(row[prop.Name], propertyInfo.PropertyType), null);
}


试试这段代码,

i我很长时间使用这个。



try this code,
i am using this for long time.

public static List<T> DataTableToList<T>(DataTable dt) where T : class, new()
{
    List<T> lstItems = new List<T>();
    if (dt != null && dt.Rows.Count > 0)
        foreach (DataRow row in dt.Rows)
            lstItems.Add(ConvertDataRowToGenericType<T>(row));
    else
        lstItems = null;
    return lstItems;
}

private static T ConvertDataRowToGenericType<T>(DataRow row) where T : class,new()
{
    Type entityType = typeof(T);
    T objEntity = new T();
    foreach (DataColumn column in row.Table.Columns)
    {
        object value = row[column.ColumnName];
        if (value == DBNull.Value) value = null;
        PropertyInfo property = entityType.GetProperty(column.ColumnName, BindingFlags.Instance | BindingFlags.IgnoreCase | BindingFlags.Public);
        try
        {
            if (property != null && property.CanWrite)
                property.SetValue(objEntity, value, null);

        }
        catch (Exception ex)
        {
           throw ex;
        }
    }
    return objEntity;
}


这篇关于如何处理null-able类型将datatable转换为list的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆