In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
这篇文章将为大家详细讲解有关C#中如何使用DataTable通过反射转实体类,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。
using System;using System.Collections.Generic;using System.Data;using System.Linq;using System.Text;namespace MySqlHelper{ public static class DataTableToModel { /// /// DataTable通过反射获取单个像 /// public static T ToSingleModel(this DataTable data) where T : new() { try { T t = data.GetList(null, true).Single(); return t; } catch (Exception e) { return new T(); } } /// /// DataTable通过反射获取单个像 /// 前缀 /// 是否忽略大小写,默认不区分 /// public static T ToSingleModel(this DataTable data, string prefix, bool ignoreCase = true) where T : new() { T t = data.GetList(prefix, ignoreCase).Single(); return t; } /// /// DataTable通过反射获取多个对像 /// /// /// /// public static List ToListModel(this DataTable data) where T : new() { List t = data.GetList(null, true); return t; } /// /// DataTable通过反射获取多个对像 /// /// 前缀 /// 是否忽略大小写,默认不区分 /// private static List ToListModel(this DataTable data, string prefix, bool ignoreCase = true) where T : new() { List t = data.GetList(prefix, ignoreCase); return t; } private static List GetList(this DataTable data, string prefix, bool ignoreCase = true) where T : new() { List t = new List(); int columnscount = data.Columns.Count; if (ignoreCase) { for (int i = 0; i < columnscount; i++) data.Columns[i].ColumnName = data.Columns[i].ColumnName.ToUpper(); } try { var properties = new T().GetType().GetProperties(); var rowscount = data.Rows.Count; for (int i = 0; i < rowscount; i++) { var model = new T(); foreach (var p in properties) { var keyName = prefix + p.Name + ""; if (ignoreCase) keyName = keyName.ToUpper(); for (int j = 0; j < columnscount; j++) { if (data.Columns[j].ColumnName == keyName && data.Rows[i][j] != null) { string pval = data.Rows[i][j].ToString(); if (!string.IsNullOrEmpty(pval)) { try { // We need to check whether the property is NULLABLE if (p.PropertyType.IsGenericType && p.PropertyType.GetGenericTypeDefinition() == typeof(Nullable)) { p.SetValue(model, Convert.ChangeType(data.Rows[i][j], p.PropertyType.GetGenericArguments()[0]), null); } else { p.SetValue(model, Convert.ChangeType(data.Rows[i][j], p.PropertyType), null); } } catch (Exception x) { throw x; } } break; } } } t.Add(model); } } catch (Exception ex) { throw ex; } return t; } }}关于"C#中如何使用DataTable通过反射转实体类"这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 227
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.