2025-02-10 20:53:40 +08:00
|
|
|
|
using System.Collections;
|
2024-09-22 11:05:41 +08:00
|
|
|
|
using System.Data;
|
|
|
|
|
|
|
2025-02-10 20:53:40 +08:00
|
|
|
|
namespace ShrlAlgo.Toolkit.Core.Extensions;
|
2024-09-22 11:05:41 +08:00
|
|
|
|
|
|
|
|
|
|
public static class DataTableExtensions
|
|
|
|
|
|
{
|
2024-12-22 10:26:12 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 获取数据
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="dt"></param>
|
|
|
|
|
|
/// <param name="row"></param>
|
|
|
|
|
|
/// <param name="column"></param>
|
|
|
|
|
|
/// <returns></returns>
|
2024-09-22 11:05:41 +08:00
|
|
|
|
public static string GetValue(this DataTable dt, int row, int column)
|
2024-12-22 10:26:12 +08:00
|
|
|
|
{
|
|
|
|
|
|
return dt != null ? dt.Rows[row][column].ToString() : string.Empty;
|
|
|
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 获取值
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="dr"></param>
|
|
|
|
|
|
/// <param name="column"></param>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
/// <exception cref="ArgumentNullException"></exception>
|
2024-09-22 11:05:41 +08:00
|
|
|
|
public static string GetValue(this DataRow dr, int column)
|
2024-12-22 10:26:12 +08:00
|
|
|
|
{
|
|
|
|
|
|
return dr == null ? throw new ArgumentNullException(nameof(dr)) : dr[column].ToString();
|
|
|
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 设置值
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="dt"></param>
|
|
|
|
|
|
/// <param name="row"></param>
|
|
|
|
|
|
/// <param name="column"></param>
|
|
|
|
|
|
/// <param name="value"></param>
|
2024-09-22 11:05:41 +08:00
|
|
|
|
public static void SetValue(this DataTable dt, int row, int column, object value)
|
2024-12-22 10:26:12 +08:00
|
|
|
|
{
|
|
|
|
|
|
dt.Rows[row][column] = value;
|
|
|
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 设置值
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="dr"></param>
|
|
|
|
|
|
/// <param name="column"></param>
|
|
|
|
|
|
/// <param name="value"></param>
|
|
|
|
|
|
/// <exception cref="ArgumentNullException"></exception>
|
2024-09-22 11:05:41 +08:00
|
|
|
|
public static void SetValue(this DataRow dr, int column, object value)
|
2024-12-22 10:26:12 +08:00
|
|
|
|
{
|
|
|
|
|
|
if (dr == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new ArgumentNullException(nameof(dr));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
dr[column] = value;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 转实体,属性名必须与列名相同
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <typeparam name="T"></typeparam>
|
|
|
|
|
|
/// <param name="table"></param>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
public static List<T> ToEntities<T>(this DataTable table)
|
|
|
|
|
|
where T : new()
|
|
|
|
|
|
{
|
|
|
|
|
|
var entities = new List<T>();
|
|
|
|
|
|
if (table == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
return null;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
foreach (DataRow row in table.Rows)
|
|
|
|
|
|
{
|
|
|
|
|
|
T entity = new();
|
|
|
|
|
|
foreach (var item in entity.GetType().GetProperties())
|
|
|
|
|
|
{
|
|
|
|
|
|
if (table.Columns.Contains(item.Name))
|
|
|
|
|
|
{
|
|
|
|
|
|
if (DBNull.Value != row[item.Name])
|
|
|
|
|
|
{
|
|
|
|
|
|
var newType = item.PropertyType;
|
|
|
|
|
|
//判断type类型是否为泛型,因为nullable是泛型类,
|
|
|
|
|
|
if (newType.IsGenericType && newType.GetGenericTypeDefinition() == typeof(Nullable<>)) //判断convertsionType是否为nullable泛型类
|
|
|
|
|
|
{
|
|
|
|
|
|
//如果type为nullable类,声明一个NullableConverter类,该类提供从Nullable类到基础基元类型的转换
|
|
|
|
|
|
var nullableConverter = new System.ComponentModel.NullableConverter(newType);
|
|
|
|
|
|
//将type转换为nullable对的基础基元类型
|
|
|
|
|
|
newType = nullableConverter.UnderlyingType;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
item.SetValue(entity, Convert.ChangeType(row[item.Name], newType), null);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
entities.Add(entity);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return entities;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 将指定的集合转换成DataTable。
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="list">将指定的集合。</param>
|
|
|
|
|
|
/// <returns>返回转换后的DataTable。</returns>
|
|
|
|
|
|
public static DataTable ToDataTable(this IList list)
|
|
|
|
|
|
{
|
|
|
|
|
|
var table = new DataTable();
|
|
|
|
|
|
|
|
|
|
|
|
var properties = list[0].GetType().GetProperties();
|
|
|
|
|
|
foreach (var pi in properties)
|
|
|
|
|
|
{
|
|
|
|
|
|
var pt = pi.PropertyType;
|
|
|
|
|
|
if (pt.IsGenericType && pt.GetGenericTypeDefinition() == typeof(Nullable<>))
|
|
|
|
|
|
{
|
|
|
|
|
|
pt = pt.GetGenericArguments()[0];
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
table.Columns.Add(new DataColumn(pi.Name, pt));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
for (var i = 0; i < list.Count; i++)
|
|
|
|
|
|
{
|
|
|
|
|
|
var tempList = new ArrayList();
|
|
|
|
|
|
foreach (var pi in properties)
|
|
|
|
|
|
{
|
|
|
|
|
|
var obj = pi.GetValue(list[i], null);
|
|
|
|
|
|
tempList.Add(obj);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var array = tempList.ToArray();
|
|
|
|
|
|
table.LoadDataRow(array, true);
|
|
|
|
|
|
}
|
|
|
|
|
|
return table;
|
|
|
|
|
|
}
|
2024-09-22 11:05:41 +08:00
|
|
|
|
}
|