【问题标题】:How to convert a list into data table [duplicate]如何将列表转换为数据表[重复]
【发布时间】:2013-08-08 16:13:21
【问题描述】:

我有一个包含一些属性的数据列表。我想将该列表数据转换为数据表。如何将列表转换为数据表。

【问题讨论】:

  • 您能给我们提供有关列表中对象类型的信息吗?

标签: c# asp.net


【解决方案1】:

添加这个函数并调用它,它会将List 转换为DataTable

public static DataTable ToDataTable<T>(List<T> items)
{
        DataTable dataTable = new DataTable(typeof(T).Name);
    
        //Get all the properties
        PropertyInfo[] Props = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);
        foreach (PropertyInfo prop in Props)
        {
            //Defining type of data column gives proper data table 
            var type = (prop.PropertyType.IsGenericType && prop.PropertyType.GetGenericTypeDefinition() == typeof(Nullable<>) ? Nullable.GetUnderlyingType(prop.PropertyType) : prop.PropertyType);
            //Setting column names as Property names
            dataTable.Columns.Add(prop.Name, type);
        }
        foreach (T item in items)
        {
           var values = new object[Props.Length];
           for (int i = 0; i < Props.Length; i++)
           {
                //inserting property values to datatable rows
                values[i] = Props[i].GetValue(item, null);
           }
           dataTable.Rows.Add(values);
      }
      //put a breakpoint here and check datatable
      return dataTable;
}

【讨论】:

  • 这个函数怎么调用,谢谢回复
  • 假设你有一个 User.List rm = new List();在 User 类中有 FirstName、LastName、Address 等属性。然后将此列表转换为 datatable ,只需使用此 DataTable UserDt = rm.ToDataTable();
  • 我们是否需要添加任何命名空间才能使此方法起作用? PropertyInfo[] 未被识别。
  • 好的,我明白了,system.reflection;
  • 单击下面有红线的类名(如语法错误),然后按 ALT+SHIFT+F10。它将向您显示名称空间。选择最上面的 System.Reflection。
【解决方案2】:

您可以使用此扩展方法并像这样调用它。

DataTable dt =   YourList.ToDataTable();

public static DataTable ToDataTable<T>(this List<T> iList)
        {
            DataTable dataTable = new DataTable();
            PropertyDescriptorCollection propertyDescriptorCollection =
                TypeDescriptor.GetProperties(typeof(T));
            for (int i = 0; i < propertyDescriptorCollection.Count; i++)
            {
                PropertyDescriptor propertyDescriptor = propertyDescriptorCollection[i];
                Type type = propertyDescriptor.PropertyType;

                if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable<>))
                    type = Nullable.GetUnderlyingType(type);


                dataTable.Columns.Add(propertyDescriptor.Name, type);
            }
            object[] values = new object[propertyDescriptorCollection.Count];
            foreach (T iListItem in iList)
            {
                for (int i = 0; i < values.Length; i++)
                {
                    values[i] = propertyDescriptorCollection[i].GetValue(iListItem);
                }
                dataTable.Rows.Add(values);
            }
            return dataTable;
        }

【讨论】:

  • 正是我想要的!
  • 这给了我List string 类型的数值。
【解决方案3】:
private DataTable CreateDataTable(IList<T> item)
{
    Type type = typeof(T);
    var properties = type.GetProperties();

    DataTable dataTable = new DataTable();
    foreach (PropertyInfo info in properties)
    {
        dataTable.Columns.Add(new DataColumn(info.Name, Nullable.GetUnderlyingType(info.PropertyType) ?? info.PropertyType));
    }

    foreach (T entity in item)
    {
        object[] values = new object[properties.Length];
        for (int i = 0; i < properties.Length; i++)
        {
            values[i] = properties[i].GetValue(entity);
        }

        dataTable.Rows.Add(values);
    }
    return dataTable;
}

【讨论】:

猜你喜欢
  • 2018-10-23
  • 2019-10-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-27
  • 1970-01-01
相关资源
最近更新 更多