当前位置: 首页 > backend >正文

Datatable和实体集合互转

1.使用已废弃的 JavaScriptSerializer,且反序列化为弱类型 ArrayList。可用但不推荐。

using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Reflection;
using System.Web;
using System.Web.Script.Serialization;namespace Helper
{public static class DataTableHelper{// 将JSON字符串转换为DataTablepublic static DataTable JsonToDataTable(string json){DataTable dataTable = new DataTable();try{JavaScriptSerializer javaScriptSerializer = new JavaScriptSerializer();javaScriptSerializer.MaxJsonLength = Int32.MaxValue;ArrayList arrayList = javaScriptSerializer.Deserialize<ArrayList>(json);if (arrayList.Count > 0){foreach (Dictionary<string, object> dic in arrayList){if (dic.Keys.Count == 0) return dataTable;if (dataTable.Columns.Count == 0){foreach (string current in dic.Keys){Type tp = dic[current]?.GetType() ?? typeof(string);dataTable.Columns.Add(current, tp);}}DataRow datarow = dataTable.NewRow();foreach (string current in dic.Keys){datarow[current] = dic[current] ?? DBNull.Value;}dataTable.Rows.Add(datarow);}}}catch { }return dataTable;}// 将DataTable转换为泛型实体列表public static List<T> ConvertToEntity<T>(this DataTable table) where T : new(){List<T> list = new List<T>();foreach (DataRow row in table.Rows){T entity = new T();foreach (PropertyInfo prop in typeof(T).GetProperties()){if (table.Columns.Contains(prop.Name.ToUpper())){object value = row[prop.Name.ToUpper()];if (value != DBNull.Value){// 处理decimal/double转string的特殊情况if ((prop.PropertyType == typeof(string)) &&(value is decimal || value is double)){prop.SetValue(entity, Convert.ChangeType(value, prop.PropertyType));}else if ((prop.PropertyType == typeof(int) || prop.PropertyType == typeof(long)) &&(value is decimal || value is double)){prop.SetValue(entity, Convert.ChangeType(value, prop.PropertyType));}else{prop.SetValue(entity, value);}}}}list.Add(entity);}return list;}// 将DataRow转换为泛型实体public static T ConvertToEntity<T>(this DataRow row) where T : new(){DataTable dt = new DataTable();dt.Rows.Add(row);return ConvertToEntity<T>(dt).FirstOrDefault();}// 将泛型列表转换为DataTablepublic static DataTable ConvertToDataTable<T>(this List<T> list){DataTable table = new DataTable();if (list == null || list.Count == 0) return table;foreach (PropertyInfo prop in typeof(T).GetProperties()){table.Columns.Add(prop.Name.ToUpper(),Nullable.GetUnderlyingType(prop.PropertyType) ?? prop.PropertyType);}foreach (T entity in list){DataRow row = table.NewRow();foreach (PropertyInfo prop in typeof(T).GetProperties()){object value = prop.GetValue(entity, null);row[prop.Name.ToUpper()] = value ?? DBNull.Value;}table.Rows.Add(row);}return table;}// 将泛型模型转换为DataRowpublic static DataRow ConvertToDataRow<T>(this T model){DataTable table = new DataTable();if (model == null) return null;foreach (PropertyInfo prop in typeof(T).GetProperties()){table.Columns.Add(prop.Name.ToUpper(),Nullable.GetUnderlyingType(prop.PropertyType) ?? prop.PropertyType);}DataRow row = table.NewRow();foreach (PropertyInfo prop in typeof(T).GetProperties()){object value = prop.GetValue(model, null);row[prop.Name.ToUpper()] = value ?? DBNull.Value;}table.Rows.Add(row);return table.Rows[0];}}
}

2.使用 Newtonsoft.Json.Linq.JArray 解析 JSON,支持复杂结构和动态类型。推荐

using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Reflection;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;namespace Helper
{public static class DataTableHelper{// 将JSON字符串转换为DataTablepublic static DataTable JsonToDataTable(string json){var dataTable = new DataTable();try{var jsonArray = JArray.Parse(json);if (jsonArray.Count == 0) return dataTable;// 初始化列var firstItem = jsonArray.First.ToObject<Dictionary<string, object>>();foreach (var key in firstItem.Keys){dataTable.Columns.Add(key, GetDataType(firstItem[key]));}// 填充数据foreach (var item in jsonArray){var row = dataTable.NewRow();foreach (var key in item.Properties()){var columnName = key.Name;var value = key.Value?.ToString() ?? DBNull.Value;row[columnName] = ConvertValue(value, dataTable.Columns[columnName].DataType);}dataTable.Rows.Add(row);}}catch (Exception ex){// 记录异常(实际项目中使用日志组件)Console.WriteLine($"JSON转DataTable失败: {ex.Message}");}return dataTable;}// 将DataTable转换为泛型实体列表public static List<T> ConvertToEntity<T>(this DataTable table) where T : new(){var properties = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance).ToList();var list = new List<T>();foreach (DataRow row in table.Rows){var entity = new T();foreach (var prop in properties){if (!table.Columns.Contains(prop.Name) && !table.Columns.Contains(prop.Name.ToUpper()))continue;var columnName = table.Columns[prop.Name] != null ? prop.Name : table.Columns.Cast<DataColumn>().FirstOrDefault(c => c.ColumnName.Equals(prop.Name, StringComparison.OrdinalIgnoreCase))?.ColumnName;if (columnName == null) continue;var value = row[columnName];if (value == DBNull.Value){if (prop.PropertyType.IsValueType && Nullable.GetUnderlyingType(prop.PropertyType) == null)continue; // 跳过非可空值类型的DBNullvalue = null;}try{prop.SetValue(entity, Convert.ChangeType(value, prop.PropertyType));}catch (Exception ex){Console.WriteLine($"属性 {prop.Name} 转换失败: {ex.Message}");}}list.Add(entity);}return list;}// 将DataRow转换为泛型实体public static T ConvertToEntity<T>(this DataRow row) where T : new(){var dt = new DataTable();dt.Rows.Add(row.ItemArray);return dt.ConvertToEntity<T>().FirstOrDefault();}// 将泛型列表转换为DataTablepublic static DataTable ConvertToDataTable<T>(this List<T> list){if (list == null || list.Count == 0) return new DataTable();var dataTable = new DataTable();var properties = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);foreach (var prop in properties){dataTable.Columns.Add(prop.Name, Nullable.GetUnderlyingType(prop.PropertyType) ?? prop.PropertyType);}foreach (var entity in list){var row = dataTable.NewRow();foreach (var prop in properties){var value = prop.GetValue(entity) ?? DBNull.Value;row[prop.Name] = value;}dataTable.Rows.Add(row);}return dataTable;}// 辅助方法:获取值的类型private static Type GetDataType(object value){if (value == null) return typeof(string);return value.GetType();}// 辅助方法:安全转换值类型private static object ConvertValue(object value, Type targetType){if (value == DBNull.Value) return null;if (targetType.IsEnum)return Enum.Parse(targetType, value.ToString());if (targetType == typeof(DateTime) && value is string str)return DateTime.Parse(str);return Convert.ChangeType(value, targetType);}}
}
http://www.xdnf.cn/news/10176.html

相关文章:

  • 华锐视点助力,虚拟旅游绽放更璀璨光彩​
  • 图书管理系统的设计与实现
  • 北京大学肖臻老师《区块链技术与应用》公开课:06-BTC-网络
  • canoe 排查配置相关【graphics,capl】
  • Python基本运算符
  • python装饰器
  • DSP处理数字信号做什么用的?
  • Unsafe.putOrderedInt与Volatile
  • 驱动灯珠芯片LT3743手册理解
  • phpmyadmin
  • RTOS:启动调度器的作用(含源码逐行解读)
  • 微信小店推客系统达人用户管理的数据支持和便利
  • 【仿生机器人】Alice计划——仿生机器人需求
  • ABB HIEE300690R0001 AR C093 AE01 励磁调节器 PCB板特价
  • 第六十一节:深度学习-使用 OpenCV DNN 模块
  • 江科大SPI串行外设接口hal库实现
  • Linux 1.0.4
  • [硬件选型篇] 一文解决常用5V转3.3V电路选型困难(包括各选型的优缺点、纹波、效率等)
  • DAY 15 复习日
  • SpringBoot整合Flowable【08】- 前后端如何交互
  • jq处理日志数据
  • 局域网/内网IP地址配置HTTPS证书全流程指南
  • TypeScript 中高级类型 keyof 与 typeof的场景剖析。
  • [STM32问题解决(2)]STM32通过串口与PC通信,打开串口助手后无法在打开状态下下载程序和复位STM32
  • 抢占先机!品牌如何利用软文营销领跑内容营销赛道?
  • 【笔记】Windows 系统安装 Supabase CLI 完整指南(基于 Scoop)
  • 未来技术展望
  • jmeter:登录接口的token用于下一个接口
  • Co-IP—验证蛋白互作的不二之选
  • JavaSwing之--ImageImageIcon