Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/elixir/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何将列表转换为数据表_C#_Asp.net - Fatal编程技术网

C# 如何将列表转换为数据表

C# 如何将列表转换为数据表,c#,asp.net,C#,Asp.net,我有一个带有一些属性的数据列表。我想把列表数据转换成数据表。如何将列表转换为数据表 您可以使用此扩展方法并像这样调用它 DataTable dt = YourList.ToDataTable(); public static DataTable ToDataTable<T>(this List<T> iList) { DataTable dataTable = new DataTable(); Prop

我有一个带有一些属性的数据列表。我想把列表数据转换成数据表。如何将列表转换为数据表

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

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;
        }
DataTable dt=YourList.ToDataTable();
公共静态数据表ToDataTable(此列表为iList)
{
DataTable=新的DataTable();
属性描述集合属性描述集合=
GetProperties(typeof(T));
对于(int i=0;i
只需添加此函数并调用它,它就会将列表转换为数据表

公共静态数据表到数据表(列表项)
{
DataTable=新的DataTable(typeof(T).Name);
//获取所有属性
PropertyInfo[]Props=typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);
foreach(PropertyInfo Props in Props)
{
//定义数据列的类型可以提供适当的数据表
变量类型=(prop.PropertyType.IsGenericType&&prop.PropertyType.GetGenericTypeDefinition()==typeof(可空)?可空。getUnderlineType(prop.PropertyType):prop.PropertyType);
//将列名设置为属性名
dataTable.Columns.Add(prop.Name,type);
}
foreach(项目中的T项目)
{
var值=新对象[Props.Length];
for(int i=0;i
专用数据表CreateDataTable(IList项)
{
类型=类型(T);
var properties=type.GetProperties();
DataTable=新的DataTable();
foreach(属性中的属性信息)
{
dataTable.Columns.Add(新的DataColumn(info.Name,null.GetUnderlyingType(info.PropertyType)??info.PropertyType));
}
foreach(项目中的T实体)
{
object[]value=新对象[properties.Length];
for(int i=0;i
您能给我们提供有关列表中对象类型的信息吗?如何调用此函数。感谢您的回复支持您拥有用户列表。list rm=new list();在用户类中有FirstName、LastName、Address等属性。然后,要将此列表转换为datatable,只需使用此datatable UserDt=rm.ToDataTable();我们需要添加任何名称空间才能使此方法工作吗?PropertyInfo[]未被识别。好的,我知道了,system.reflection;单击下面有红线的类名(如语法错误中所示),然后按ALT+SHIFT+F10。它将向您显示名称空间。选择顶部的一个,即System.Reflection.
ConvertListToDataTable(…)
在dotnet中不为人所知,除非您访问此页面,在该页面中可以找到此方法的代码`正是我所寻找的!这给了我
字符串类型的
列表
的数值。
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;
}
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;
}