Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/293.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# 将列表转换为DataTable后缺少数据_C# - Fatal编程技术网

C# 将列表转换为DataTable后缺少数据

C# 将列表转换为DataTable后缺少数据,c#,C#,我创建了一个新的Guid列表,如下所示: List<Guid> EmpGuid = new List<Guid>(); 一旦有了项目,我就将其转换为一个数据表: var parameters = ToDataTable(EmpGuid); ToDataTable方法: public static DataTable ToDataTable<T>(IList<T> data) { PropertyDescriptorCollection

我创建了一个新的
Guid
列表,如下所示:

List<Guid> EmpGuid = new List<Guid>();
一旦有了项目,我就将其转换为一个
数据表

var parameters = ToDataTable(EmpGuid);
ToDataTable
方法:

public static DataTable ToDataTable<T>(IList<T> data)
{
    PropertyDescriptorCollection props = TypeDescriptor.GetProperties(typeof(T));

    DataTable table = new DataTable();

    for (int i = 0; i < props.Count; i++)
    {
        PropertyDescriptor prop = props[i];
        table.Columns.Add(prop.Name, prop.PropertyType);
    }

    object[] values = new object[props.Count];

    foreach (T item in data)
    {
        for (int i = 0; i < values.Length; i++)
        {
            values[i] = props[i].GetValue(item);
        }

        table.Rows.Add(values);
    }

    return table;
}
公共静态数据表到数据表(IList数据)
{
PropertyDescriptorCollection props=TypeDescriptor.GetProperties(typeof(T));
DataTable=新的DataTable();
for(int i=0;i
问题是,当我将其转换为
DataTable
时,它会创建2行,但有空行没有我的
EmpGuid
值。有人知道为什么会这样吗


关于

只需添加此函数并调用它,它就会将列表转换为数据表

 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);
            if (Props.Length > 0)
            {
                foreach (PropertyInfo prop in Props)
                {
                    if (GetDefault(prop.PropertyType.FullName) != null)
                    {
                        //Setting column names as Property names
                        dataTable.Columns.Add(prop.Name, Nullable.GetUnderlyingType(prop.PropertyType) ?? prop.PropertyType);
                    }
                }
            }
            else
            {
                dataTable.Columns.Add("myColName");
            }
            foreach (T item in items)
            {
                if (item != null)
                {
                    DataRow dr = dataTable.NewRow();
                    if (Props.Length > 0)
                    {
                        foreach (PropertyInfo prop in Props)
                        {
                            if (GetDefault(prop.PropertyType.FullName) != null)
                            {
                                //inserting property values to datatable rows
                                dr[prop.Name] = prop.GetValue(item, null) ?? GetDefault(prop.PropertyType.FullName);
                            }
                        }
                    }
                    else
                    {
                            //inserting property values to datatable rows
                            dr[0] = item;
                    }

                    dataTable.Rows.Add(dr);
                }
            }

            //put a breakpoint here and check datatable

            return dataTable;

        }

        public static object GetDefault(string dataType)
        {

            if (dataType.Contains("System.String"))
            {
                return string.Empty;
            }
            if (dataType.Contains("System.Boolean"))
            {
                return false;
            }
            if (dataType.Contains("System.Decimal"))
            {
                return 0.0;
            }
            if (dataType.Contains("System.DateTime"))
            {
                return DateTime.MinValue;
            }
            if (dataType.Contains("System.Int64"))
            {
                return 0;
            }
            if (dataType.Contains("System.Guid"))
            {
                return null;
            }
            if (dataType.Contains("System.Int16"))
            {
                return 0;
            }
            if (dataType.Contains("Int32"))
            {
                return 0;

            }
            if (dataType.Contains("System.Object"))
            {
                return null;
            }

            return null;
        }
公共静态数据表到数据表(列表项)
{
DataTable=新的DataTable(typeof(T).Name);
//获取所有属性
PropertyInfo[]Props=typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);
如果(道具长度>0)
{
foreach(PropertyInfo Props in Props)
{
if(GetDefault(prop.PropertyType.FullName)!=null)
{
//将列名设置为属性名
dataTable.Columns.Add(prop.Name,Nullable.GetUnderlyingType(prop.PropertyType)??prop.PropertyType);
}
}
}
其他的
{
dataTable.Columns.Add(“myColName”);
}
foreach(项目中的T项目)
{
如果(项!=null)
{
DataRow dr=dataTable.NewRow();
如果(道具长度>0)
{
foreach(PropertyInfo Props in Props)
{
if(GetDefault(prop.PropertyType.FullName)!=null)
{
//将属性值插入数据表行
dr[prop.Name]=prop.GetValue(item,null)??GetDefault(prop.PropertyType.FullName);
}
}
}
其他的
{
//将属性值插入数据表行
dr[0]=项目;
}
dataTable.Rows.Add(dr);
}
}
//在此处放置断点并检查datatable
返回数据表;
}
公共静态对象GetDefault(字符串数据类型)
{
if(dataType.Contains(“System.String”))
{
返回字符串。空;
}
if(dataType.Contains(“System.Boolean”))
{
返回false;
}
if(dataType.Contains(“System.Decimal”))
{
返回0.0;
}
if(dataType.Contains(“System.DateTime”))
{
return DateTime.MinValue;
}
if(dataType.Contains(“System.Int64”))
{
返回0;
}
if(dataType.Contains(“System.Guid”))
{
返回null;
}
if(dataType.Contains(“System.Int16”))
{
返回0;
}
if(dataType.Contains(“Int32”))
{
返回0;
}
if(dataType.Contains(“System.Object”))
{
返回null;
}
返回null;
}

我尝试了,但
ItemArray
仍为空
 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);
            if (Props.Length > 0)
            {
                foreach (PropertyInfo prop in Props)
                {
                    if (GetDefault(prop.PropertyType.FullName) != null)
                    {
                        //Setting column names as Property names
                        dataTable.Columns.Add(prop.Name, Nullable.GetUnderlyingType(prop.PropertyType) ?? prop.PropertyType);
                    }
                }
            }
            else
            {
                dataTable.Columns.Add("myColName");
            }
            foreach (T item in items)
            {
                if (item != null)
                {
                    DataRow dr = dataTable.NewRow();
                    if (Props.Length > 0)
                    {
                        foreach (PropertyInfo prop in Props)
                        {
                            if (GetDefault(prop.PropertyType.FullName) != null)
                            {
                                //inserting property values to datatable rows
                                dr[prop.Name] = prop.GetValue(item, null) ?? GetDefault(prop.PropertyType.FullName);
                            }
                        }
                    }
                    else
                    {
                            //inserting property values to datatable rows
                            dr[0] = item;
                    }

                    dataTable.Rows.Add(dr);
                }
            }

            //put a breakpoint here and check datatable

            return dataTable;

        }

        public static object GetDefault(string dataType)
        {

            if (dataType.Contains("System.String"))
            {
                return string.Empty;
            }
            if (dataType.Contains("System.Boolean"))
            {
                return false;
            }
            if (dataType.Contains("System.Decimal"))
            {
                return 0.0;
            }
            if (dataType.Contains("System.DateTime"))
            {
                return DateTime.MinValue;
            }
            if (dataType.Contains("System.Int64"))
            {
                return 0;
            }
            if (dataType.Contains("System.Guid"))
            {
                return null;
            }
            if (dataType.Contains("System.Int16"))
            {
                return 0;
            }
            if (dataType.Contains("Int32"))
            {
                return 0;

            }
            if (dataType.Contains("System.Object"))
            {
                return null;
            }

            return null;
        }