-
LIst和table的转换
- public static class DataTableExtensions
- {
-
-
-
-
-
-
-
-
-
- public static DataTable ToDataTable<T>(this IEnumerable<T> list)
- {
-
-
- List<PropertyInfo> pList = new List<PropertyInfo>();
-
-
- Type type = typeof(T);
- DataTable dt = new DataTable();
-
- Array.ForEach<PropertyInfo>(type.GetProperties(), p => { pList.Add(p); dt.Columns.Add(p.Name, p.PropertyType); });
- foreach (var item in list)
- {
-
- DataRow row = dt.NewRow();
-
- pList.ForEach(p => row[p.Name] = p.GetValue(item, null));
-
- dt.Rows.Add(row);
- }
- return dt;
- }
-
-
-
-
-
-
-
-
- public static List<T> ToList<T>(this DataTable dt) where T : class, new()
- {
-
- List<PropertyInfo> prlist = new List<PropertyInfo>();
-
-
- Type t = typeof(T);
-
-
- Array.ForEach<PropertyInfo>(t.GetProperties(), p => { if (dt.Columns.IndexOf(p.Name) != -1) prlist.Add(p); });
-
-
-
- List<T> oblist = new List<T>();
-
- foreach (DataRow row in dt.Rows)
- {
-
- T ob = new T();
-
- prlist.ForEach(p => { if (row[p.Name] != DBNull.Value) p.SetValue(ob, row[p.Name], null); });
-
- oblist.Add(ob);
- }
- return oblist;
- }
-
-
-
-
-
-
-
-
-
- public static DataTable ToDataTableTow(IList list)
- {
- DataTable result = new DataTable();
- if (list.Count > 0)
- {
- PropertyInfo[] propertys = list[0].GetType().GetProperties();
-
- foreach (PropertyInfo pi in propertys)
- {
- result.Columns.Add(pi.Name, pi.PropertyType);
- }
- for (int i = 0; i < list.Count; i++)
- {
- ArrayList tempList = new ArrayList();
- foreach (PropertyInfo pi in propertys)
- {
- object obj = pi.GetValue(list[i], null);
- tempList.Add(obj);
- }
- object[] array = tempList.ToArray();
- result.LoadDataRow(array, true);
- }
- }
- return result;
- }
-
-
-
-
-
-
-
-
-
-
-
-
- public static DataTable ToDataTable<T>(IList<T> list)
- {
- return ToDataTable<T>(list, null);
-
- }
-
-
-
-
-
-
-
-
-
-
-
- public static DataTable ToDataTable<T>(IList<T> list, params string[] propertyName)
- {
- List<string> propertyNameList = new List<string>();
- if (propertyName != null)
- propertyNameList.AddRange(propertyName);
- DataTable result = new DataTable();
- if (list.Count > 0)
- {
- PropertyInfo[] propertys = list[0].GetType().GetProperties();
- foreach (PropertyInfo pi in propertys)
- {
- if (propertyNameList.Count == 0)
- {
- result.Columns.Add(pi.Name, pi.PropertyType);
- }
- else
- {
- if (propertyNameList.Contains(pi.Name))
- result.Columns.Add(pi.Name, pi.PropertyType);
- }
- }
-
- for (int i = 0; i < list.Count; i++)
- {
- ArrayList tempList = new ArrayList();
- foreach (PropertyInfo pi in propertys)
- {
- if (propertyNameList.Count == 0)
- {
- object obj = pi.GetValue(list[i], null);
- tempList.Add(obj);
- }
- else
- {
- if (propertyNameList.Contains(pi.Name))
- {
- object obj = pi.GetValue(list[i], null);
- tempList.Add(obj);
- }
- }
- }
- object[] array = tempList.ToArray();
- result.LoadDataRow(array, true);
- }
- }
- return result;
- }
- }
-
相关阅读:
个人项目作业
软件工程个人博客作业
软件工程热身作业
OO第四单元作业总结
OO第三单元作业总结
OO第二单元作业总结
酸甜苦辣皆阅历,悲欢离合尽人生——软件工程个人总结
Centos7里yum出问题可以试试
idea 报错 Two modules in a project cannot share the same content root
关于本地git的补充
-
原文地址:https://www.cnblogs.com/kexb/p/4551905.html
Copyright © 2020-2023
润新知