List转换为DataTable

 List<DocFlowInfo> listdoc = flowQueryBO.QueryProject(condition).Items.ToList();
                DataTable result = new DataTable();
                if (listdoc != null && listdoc.Count > 0)
                {
                    PropertyInfo[] propertys = listdoc[0].GetType().GetProperties();
                    foreach (PropertyInfo pi in propertys)
                    {
                        Type colType = pi.PropertyType;
                        if ((colType.IsGenericType) && (colType.GetGenericTypeDefinition() == typeof(Nullable<>)))
                        {
                            colType = colType.GetGenericArguments()[0];
                        }
                        result.Columns.Add(pi.Name, colType);
                    }              
                    for (int i = 0; i < listdoc.Count; i++)
                    {
                        ArrayList templist = new ArrayList();
                        foreach (PropertyInfo pi in propertys)
                        {
                            object obj = pi.GetValue(listdoc[i], null);
                            templist.Add(obj);
                        }
                        object[] array = templist.ToArray();
                        result.LoadDataRow(array, true);
                    }                
                }

posted @ 2013-12-31 11:03  心兰月湾  阅读(198)  评论(0)    收藏  举报