Files
Shrlalgo.RvKits/Sai.Toolkit.Core/Extensions/DataTableExtensions.cs

156 lines
3.7 KiB
C#
Raw Normal View History

2024-09-22 11:05:41 +08:00
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Reflection;
namespace Sai.Toolkit.Core.Extensions;
public static class DataTableExtensions
{
public static string GetValue(this DataTable dt, int row, int column)
{
return dt != null ? dt.Rows[row][column].ToString() : string.Empty;
}
public static string GetValue(this DataRow dr, int column)
{
return dr == null ? throw new ArgumentNullException() : dr[column].ToString();
}
public static void SetValue(this DataTable dt, int row, int column, object value)
{
dt.Rows[row][column] = value;
}
public static void SetValue(this DataRow dr, int column, object value)
{
if (dr == null)
{
throw new ArgumentNullException();
}
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(IList list)
{
var table = new DataTable();
if (list.Count > 0)
{
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;
}
public static DataTable ToDataTable<T>(this List<T> list)
{
var table = new DataTable();
//创建列头
var properties = typeof(T).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));
}
//创建数据行
if (list.Count > 0)
{
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;
}
}