|
@ -71,157 +71,157 @@ namespace Wood.Util |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static class DataTableHelper |
|
|
//public static class DataTableHelper
|
|
|
{ |
|
|
//{
|
|
|
/// <summary>
|
|
|
// /// <summary>
|
|
|
/// 将泛型列表转换为DataTable
|
|
|
// /// 将泛型列表转换为DataTable
|
|
|
/// </summary>
|
|
|
// /// </summary>
|
|
|
/// <typeparam name="T">实体类型</typeparam>
|
|
|
// /// <typeparam name="T">实体类型</typeparam>
|
|
|
/// <param name="list">泛型列表</param>
|
|
|
// /// <param name="list">泛型列表</param>
|
|
|
/// <param name="config">转换配置(可选)</param>
|
|
|
// /// <param name="config">转换配置(可选)</param>
|
|
|
/// <returns>DataTable</returns>
|
|
|
// /// <returns>DataTable</returns>
|
|
|
public static DataTable ToDataTable<T>(this List<T> list, Action<DataTableConfig<T>> config = null) |
|
|
// public static DataTable ToDataTable<T>(this List<T> list, Action<DataTableConfig<T>> config = null)
|
|
|
{ |
|
|
// {
|
|
|
if (list == null || list.Count == 0) |
|
|
// if (list == null || list.Count == 0)
|
|
|
return new DataTable(); |
|
|
// return new DataTable();
|
|
|
|
|
|
|
|
|
var tableConfig = new DataTableConfig<T>(); |
|
|
// var tableConfig = new DataTableConfig<T>();
|
|
|
config?.Invoke(tableConfig); |
|
|
// config?.Invoke(tableConfig);
|
|
|
|
|
|
|
|
|
var dataTable = new DataTable(); |
|
|
// var dataTable = new DataTable();
|
|
|
var properties = GetProperties(typeof(T), tableConfig); |
|
|
// var properties = GetProperties(typeof(T), tableConfig);
|
|
|
|
|
|
|
|
|
// 创建列
|
|
|
// // 创建列
|
|
|
foreach (var property in properties) |
|
|
// foreach (var property in properties)
|
|
|
{ |
|
|
// {
|
|
|
var columnType = Nullable.GetUnderlyingType(property.PropertyType) ?? property.PropertyType; |
|
|
// var columnType = Nullable.GetUnderlyingType(property.PropertyType) ?? property.PropertyType;
|
|
|
var columnName = tableConfig.ColumnMappings.TryGetValue(property.Name, out var mappedName) |
|
|
// var columnName = tableConfig.ColumnMappings.TryGetValue(property.Name, out var mappedName)
|
|
|
? mappedName |
|
|
// ? mappedName
|
|
|
: property.Name; |
|
|
// : property.Name;
|
|
|
|
|
|
|
|
|
var column = new DataColumn(columnName, columnType); |
|
|
// var column = new DataColumn(columnName, columnType);
|
|
|
dataTable.Columns.Add(column); |
|
|
// dataTable.Columns.Add(column);
|
|
|
} |
|
|
// }
|
|
|
|
|
|
|
|
|
// 填充数据
|
|
|
// // 填充数据
|
|
|
foreach (var item in list) |
|
|
// foreach (var item in list)
|
|
|
{ |
|
|
// {
|
|
|
var row = dataTable.NewRow(); |
|
|
// var row = dataTable.NewRow();
|
|
|
foreach (var property in properties) |
|
|
// foreach (var property in properties)
|
|
|
{ |
|
|
// {
|
|
|
var columnName = tableConfig.ColumnMappings.TryGetValue(property.Name, out var mappedName) |
|
|
// var columnName = tableConfig.ColumnMappings.TryGetValue(property.Name, out var mappedName)
|
|
|
? mappedName |
|
|
// ? mappedName
|
|
|
: property.Name; |
|
|
// : property.Name;
|
|
|
|
|
|
|
|
|
var value = property.GetValue(item); |
|
|
// var value = property.GetValue(item);
|
|
|
row[columnName] = value ?? DBNull.Value; |
|
|
// row[columnName] = value ?? DBNull.Value;
|
|
|
} |
|
|
// }
|
|
|
dataTable.Rows.Add(row); |
|
|
// dataTable.Rows.Add(row);
|
|
|
} |
|
|
// }
|
|
|
|
|
|
|
|
|
return dataTable; |
|
|
// return dataTable;
|
|
|
} |
|
|
// }
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
// /// <summary>
|
|
|
/// 获取需要转换的属性列表
|
|
|
// /// 获取需要转换的属性列表
|
|
|
/// </summary>
|
|
|
// /// </summary>
|
|
|
private static PropertyInfo[] GetProperties(Type type, DataTableConfig config) |
|
|
// private static PropertyInfo[] GetProperties(Type type, DataTableConfig config)
|
|
|
{ |
|
|
// {
|
|
|
var bindingFlags = BindingFlags.Public | BindingFlags.Instance; |
|
|
// var bindingFlags = BindingFlags.Public | BindingFlags.Instance;
|
|
|
if (config.IgnoreNonPublicProperties) |
|
|
// if (config.IgnoreNonPublicProperties)
|
|
|
bindingFlags &= ~BindingFlags.NonPublic; |
|
|
// bindingFlags &= ~BindingFlags.NonPublic;
|
|
|
|
|
|
|
|
|
var properties = type.GetProperties(bindingFlags); |
|
|
// var properties = type.GetProperties(bindingFlags);
|
|
|
|
|
|
|
|
|
if (config.IgnoreProperties?.Count > 0) |
|
|
// if (config.IgnoreProperties?.Count > 0)
|
|
|
{ |
|
|
// {
|
|
|
properties = Array.FindAll(properties, p => !config.IgnoreProperties.Contains(p.Name)); |
|
|
// properties = Array.FindAll(properties, p => !config.IgnoreProperties.Contains(p.Name));
|
|
|
} |
|
|
// }
|
|
|
|
|
|
|
|
|
if (config.OnlyIncludeProperties?.Count > 0) |
|
|
// if (config.OnlyIncludeProperties?.Count > 0)
|
|
|
{ |
|
|
// {
|
|
|
properties = Array.FindAll(properties, p => config.OnlyIncludeProperties.Contains(p.Name)); |
|
|
// properties = Array.FindAll(properties, p => config.OnlyIncludeProperties.Contains(p.Name));
|
|
|
} |
|
|
// }
|
|
|
|
|
|
|
|
|
return properties; |
|
|
// return properties;
|
|
|
} |
|
|
// }
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
// /// <summary>
|
|
|
/// 数据转换配置类
|
|
|
// /// 数据转换配置类
|
|
|
/// </summary>
|
|
|
// /// </summary>
|
|
|
public class DataTableConfig<T> : DataTableConfig |
|
|
// public class DataTableConfig<T> : DataTableConfig
|
|
|
{ |
|
|
// {
|
|
|
public new Dictionary<string, string> ColumnMappings { get; } = new Dictionary<string, string>(); |
|
|
// public new Dictionary<string, string> ColumnMappings { get; } = new Dictionary<string, string>();
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
// /// <summary>
|
|
|
/// 设置列映射(属性名 -> 列名)
|
|
|
// /// 设置列映射(属性名 -> 列名)
|
|
|
/// </summary>
|
|
|
// /// </summary>
|
|
|
public DataTableConfig<T> MapColumn(string propertyName, string columnName) |
|
|
// public DataTableConfig<T> MapColumn(string propertyName, string columnName)
|
|
|
{ |
|
|
// {
|
|
|
ColumnMappings[propertyName] = columnName; |
|
|
// ColumnMappings[propertyName] = columnName;
|
|
|
return this; |
|
|
// return this;
|
|
|
} |
|
|
// }
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
// /// <summary>
|
|
|
/// 设置忽略的属性
|
|
|
// /// 设置忽略的属性
|
|
|
/// </summary>
|
|
|
// /// </summary>
|
|
|
public new DataTableConfig<T> IgnoreProperty(params string[] propertyNames) |
|
|
// public new DataTableConfig<T> IgnoreProperty(params string[] propertyNames)
|
|
|
{ |
|
|
// {
|
|
|
base.IgnoreProperty(propertyNames); |
|
|
// base.IgnoreProperty(propertyNames);
|
|
|
return this; |
|
|
// return this;
|
|
|
} |
|
|
// }
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
// /// <summary>
|
|
|
/// 设置只包含的属性
|
|
|
// /// 设置只包含的属性
|
|
|
/// </summary>
|
|
|
// /// </summary>
|
|
|
public new DataTableConfig<T> OnlyIncludeProperty(params string[] propertyNames) |
|
|
// public new DataTableConfig<T> OnlyIncludeProperty(params string[] propertyNames)
|
|
|
{ |
|
|
// {
|
|
|
base.OnlyIncludeProperty(propertyNames); |
|
|
// base.OnlyIncludeProperty(propertyNames);
|
|
|
return this; |
|
|
// return this;
|
|
|
} |
|
|
// }
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
// /// <summary>
|
|
|
/// 设置是否忽略非公共属性(默认忽略)
|
|
|
// /// 设置是否忽略非公共属性(默认忽略)
|
|
|
/// </summary>
|
|
|
// /// </summary>
|
|
|
public new DataTableConfig<T> SetIgnoreNonPublicProperties(bool ignore = true) |
|
|
// public new DataTableConfig<T> SetIgnoreNonPublicProperties(bool ignore = true)
|
|
|
{ |
|
|
// {
|
|
|
base.SetIgnoreNonPublicProperties(ignore); |
|
|
// base.SetIgnoreNonPublicProperties(ignore);
|
|
|
return this; |
|
|
// return this;
|
|
|
} |
|
|
// }
|
|
|
} |
|
|
// }
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
// /// <summary>
|
|
|
/// 数据转换配置基类
|
|
|
// /// 数据转换配置基类
|
|
|
/// </summary>
|
|
|
// /// </summary>
|
|
|
public class DataTableConfig |
|
|
// public class DataTableConfig
|
|
|
{ |
|
|
// {
|
|
|
public HashSet<string> IgnoreProperties { get; } = new HashSet<string>(); |
|
|
// public HashSet<string> IgnoreProperties { get; } = new HashSet<string>();
|
|
|
public HashSet<string> OnlyIncludeProperties { get; } = new HashSet<string>(); |
|
|
// public HashSet<string> OnlyIncludeProperties { get; } = new HashSet<string>();
|
|
|
public Dictionary<string, string> ColumnMappings { get; } = new Dictionary<string, string>(); |
|
|
// public Dictionary<string, string> ColumnMappings { get; } = new Dictionary<string, string>();
|
|
|
public bool IgnoreNonPublicProperties { get; private set; } = true; |
|
|
// public bool IgnoreNonPublicProperties { get; private set; } = true;
|
|
|
|
|
|
|
|
|
public DataTableConfig IgnoreProperty(params string[] propertyNames) |
|
|
// public DataTableConfig IgnoreProperty(params string[] propertyNames)
|
|
|
{ |
|
|
// {
|
|
|
foreach (var name in propertyNames) |
|
|
// foreach (var name in propertyNames)
|
|
|
IgnoreProperties.Add(name); |
|
|
// IgnoreProperties.Add(name);
|
|
|
return this; |
|
|
// return this;
|
|
|
} |
|
|
// }
|
|
|
|
|
|
|
|
|
public DataTableConfig OnlyIncludeProperty(params string[] propertyNames) |
|
|
// public DataTableConfig OnlyIncludeProperty(params string[] propertyNames)
|
|
|
{ |
|
|
// {
|
|
|
OnlyIncludeProperties.Clear(); |
|
|
// OnlyIncludeProperties.Clear();
|
|
|
foreach (var name in propertyNames) |
|
|
// foreach (var name in propertyNames)
|
|
|
OnlyIncludeProperties.Add(name); |
|
|
// OnlyIncludeProperties.Add(name);
|
|
|
return this; |
|
|
// return this;
|
|
|
} |
|
|
// }
|
|
|
|
|
|
|
|
|
public DataTableConfig SetIgnoreNonPublicProperties(bool ignore = true) |
|
|
// public DataTableConfig SetIgnoreNonPublicProperties(bool ignore = true)
|
|
|
{ |
|
|
// {
|
|
|
IgnoreNonPublicProperties = ignore; |
|
|
// IgnoreNonPublicProperties = ignore;
|
|
|
return this; |
|
|
// return this;
|
|
|
} |
|
|
// }
|
|
|
} |
|
|
// }
|
|
|
} |
|
|
//}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|