首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >将list转换为DataTable时获取匿名类型

将list转换为DataTable时获取匿名类型
EN

Stack Overflow用户
提问于 2018-09-28 02:12:19
回答 2查看 103关注 0票数 0

我有一个列表,其中我选择了列TaskId(全局变量)和EmpGuid(从列表中),如下所示:

 var parameterstest = assignNotificationTableType.Select(x => new { TaskId, x.EmpGuid }).ToList();

因此,如果我进行调试,我会得到这样的结果:

现在我创建方法将其转换为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);
            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;
        }

但是当我尝试使用时:

 var parameters = ToDataTable(assignNotificationTableType.Select(x => new { TaskId, x.EmpGuid }).ToList());

变量返回{<>f__AnonymousType42}而不是DataTable

我不知道它为什么返回AnonymousType。有人知道那里的问题是什么吗?

更新

正如您所看到的,匿名类型datatable正确地计算行数,但ItemArray始终为零,而不是我的值

EN

回答 2

Stack Overflow用户

发布于 2018-09-28 03:24:13

我解决了这个问题,将我的Convert DataTable方法改为:

public static DataTable ToSingleDataTable<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;
        }
票数 0
EN

Stack Overflow用户

发布于 2018-09-28 02:46:11

对于dynamic变量,C#编译器添加代码以确定变量的类型并对其执行必要的操作,所有这些都在运行时完成。

C#中的泛型在编译时被具体化;也就是说,如果您有List<T>并使用new List<int>()构造它,那么在编译阶段会有一个新的完全的类(我相信,名为"List1[Int32]") will be generated and included in the binary. The invocationnew List<T>

也许你知道这是怎么回事了。要编译创建DataTable<T>的函数,编译器必须在编译时知道T的类型--这正是dynamic已经告诉编译器在运行时要延迟处理的内容。因此,每次在Tdynamic的情况下输入DataTable<T>时,编译器都必须放入一个占位符:因此是AnonymousType。调试器不知道更多,因为调试符号也是在编译时添加的。

如果您不能通过单击展开三角形来查看DataTable中包含的项,则在使用? parameters和/或? paramaters.Columns[<name>]等命令行转到调试器命令行时,您应该仍然可以查看它们

票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52542880

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档