|
@@ -1,6 +1,7 @@
|
|
|
using System;
|
|
|
using System.Collections;
|
|
|
using System.Collections.Generic;
|
|
|
+using System.ComponentModel;
|
|
|
using System.Data;
|
|
|
using System.Reflection;
|
|
|
|
|
@@ -72,72 +73,36 @@ namespace Masuit.Tools.Core.Database
|
|
|
/// </summary>
|
|
|
/// <typeparam name="T">集合项类型</typeparam>
|
|
|
/// <param name="list">集合</param>
|
|
|
+ /// <param name="tableName">表名</param>
|
|
|
/// <returns>数据集(表)</returns>
|
|
|
- public static DataTable ToDataTable<T>(this IList<T> list)
|
|
|
+ public static DataTable ToDataTable<T>(this IList<T> list, string tableName)
|
|
|
{
|
|
|
- return ToDataTable(list, null);
|
|
|
- }
|
|
|
-
|
|
|
- /// <summary>
|
|
|
- /// 将泛型集合类转换成DataTable
|
|
|
- /// </summary>
|
|
|
- /// <typeparam name="T">集合项类型</typeparam>
|
|
|
- /// <param name="list">集合</param>
|
|
|
- /// <param name="propertyName">需要返回的列的列名</param>
|
|
|
- /// <returns>数据集(表)</returns>
|
|
|
- public static DataTable ToDataTable<T>(this 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)
|
|
|
{
|
|
|
return result;
|
|
|
}
|
|
|
|
|
|
- var propertys = list[0].GetType().GetProperties();
|
|
|
- propertys.ForEach(pi =>
|
|
|
+ var properties = list[0].GetType().GetProperties();
|
|
|
+ properties.ForEach(pi =>
|
|
|
{
|
|
|
- if (propertyNameList.Count == 0)
|
|
|
- {
|
|
|
- result.Columns.Add(pi.Name, pi.PropertyType);
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
- if (propertyNameList.Contains(pi.Name))
|
|
|
- {
|
|
|
- result.Columns.Add(pi.Name, pi.PropertyType);
|
|
|
- }
|
|
|
- }
|
|
|
+ var columnName = pi.GetCustomAttribute<DescriptionAttribute>()?.Description ?? pi.Name;
|
|
|
+ result.Columns.Add(columnName, pi.PropertyType);
|
|
|
});
|
|
|
list.ForEach(item =>
|
|
|
{
|
|
|
ArrayList tempList = new ArrayList();
|
|
|
- foreach (PropertyInfo pi in propertys)
|
|
|
+ foreach (PropertyInfo pi in properties)
|
|
|
{
|
|
|
- if (propertyNameList.Count == 0)
|
|
|
- {
|
|
|
- object obj = pi.GetValue(item, null);
|
|
|
- tempList.Add(obj);
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
- if (propertyNameList.Contains(pi.Name))
|
|
|
- {
|
|
|
- object obj = pi.GetValue(item, null);
|
|
|
- tempList.Add(obj);
|
|
|
- }
|
|
|
- }
|
|
|
+ object obj = pi.GetValue(item, null);
|
|
|
+ tempList.Add(obj);
|
|
|
}
|
|
|
|
|
|
object[] array = tempList.ToArray();
|
|
|
result.LoadDataRow(array, true);
|
|
|
});
|
|
|
|
|
|
+ result.TableName = tableName;
|
|
|
return result;
|
|
|
}
|
|
|
|