C# DataTable and Model Transfer Example Code
/// <summary> /// Entity conversion auxiliary class /// </summary> public class ModelConvertHelper<T> where T : new() { /// <summary> /// List generic conversion DataTable. /// </summary> public DataTable ListToDataTable<T>(List<T> items) { var tb = new DataTable(typeof(T).Name); PropertyInfo[] props = typeof(T).GetProperties( | ); foreach (PropertyInfo prop in props) { Type t = GetCoreType(); (, t); } foreach (T item in items) { var values = new object[]; for (int i = 0; i < ; i++) { values[i] = props[i].GetValue(item, null); } (values); } return tb; } /// <summary> /// model conversion DataTable /// </summary> /// <typeparam name="T"></typeparam> /// <param name="items"></param> /// <returns></returns> public DataTable ModelToDataTable<T>(T items) { var tb = new DataTable(typeof(T).Name); PropertyInfo[] props = typeof(T).GetProperties( | ); foreach (PropertyInfo prop in props) { Type t = GetCoreType(); (, t); } var values = new object[]; for (int i = 0; i < ; i++) { values[i] = props[i].GetValue(items, null); } (values); return tb; } /// <summary> /// Determine of specified type is nullable /// </summary> public static bool IsNullable(Type t) { return ! || ( && () == typeof(Nullable<>)); } /// <summary> /// Return underlying type if type is Nullable otherwise return the type /// </summary> public static Type GetCoreType(Type t) { if (t != null && IsNullable(t)) { if (!) { return t; } else { return (t); } } else { return t; } } /// <summary> /// DataTable converts generic List /// </summary> /// <param name="dt"></param> /// <returns></returns> public static List<T> DataTableToList(DataTable dt) { // Define the collection List<T> ts = new List<T>(); // Get the type of this model Type type = typeof(T); string tempName = ""; foreach (DataRow dr in ) { T t = new T(); // Get the public properties of this model PropertyInfo[] propertys = ().GetProperties(); foreach (PropertyInfo pi in propertys) { tempName = ; // Check whether DataTable contains this column if ((tempName)) { // Determine whether this property has a Setter if (!) continue; object value = dr[tempName]; if (value != ) (t, value, null); } } (t); } return ts; } public static T DataTableToModel(DataTable dt) { // Define entities T t = new T(); // Get the type of this model Type type = typeof(T); string tempName = ""; foreach (DataRow dr in ) { // Get the public properties of this model PropertyInfo[] propertys = ().GetProperties(); foreach (PropertyInfo pi in propertys) { tempName = ; // Check whether DataTable contains this column if ((tempName)) { // Determine whether this property has a Setter if (!) continue; object value = dr[tempName]; if (value != ) (t, value, null); } } break; } return t; } }
The above is the detailed content of the sample code for the mutual transfer between C#DataTable and Model. For more information about the mutual transfer between C#DataTable and Model, please follow my other related articles!
Related Articles
C# Detailed explanation of the case of drawing PDF nested tables
Nested tables, as the name implies, are to insert one or more tables into a specific cell in a table. This article will introduce to you the code example of drawing PDF nested tables in C#. Students who need it can refer to it.2021-11-11C# sample code to implement directory jump (TreeView and SplitContainer)
This article mainly introduces the sample code for C# to implement directory jumps (TreeView and SplitContainer). The sample codes are introduced in this article in detail, which has a certain reference learning value for everyone's learning or work. Friends who need it, please learn with the editor below.2022-07-07C# implements a method to find a set of data modes
This article mainly introduces the method of C# to calculate a set of data modes. Here, the algorithm principle and implementation techniques of C# to calculate a mode is analyzed using floating-point array as an example. It has certain reference value. Friends who need it can refer to it.2015-08-08How to close the form in c#
This article mainly introduces the implementation method of closing the form in C#, which is of good reference value and hopes to be helpful to everyone. If there are any mistakes or no complete considerations, I hope you will be very grateful for your advice2023-07-07C# implements gradient gradient case for color
This article mainly introduces C# to implement gradient gradients in color, which is of good reference value and hopes to be helpful to everyone. Let's take a look with the editor2021-01-01C# proxy mode
Proxy mode: Provides a proxy for other objects to control access to other objects2012-10-10C# strings and unicode conversion practice cases
This article mainly introduces practical cases of converting C# strings and unicode, which are of good reference value and hope it will be helpful to everyone. Let's take a look with the editor2021-01-01How to remove the closing button in the upper right corner of winform
This article mainly introduces the method of removing the closing button in the upper right corner of Winform. Friends who need it can refer to it2014-02-02Unity3D realizes camera shake effect
This article mainly introduces the camera shake effect of unity3D in detail. The sample code in the article is introduced in detail and has certain reference value. Interested friends can refer to it.2020-01-01Detailed explanation of C# Socket asynchronous communication example
This article mainly introduces C# Socket asynchronous communication. The editor thinks it is quite good. I will share it with you now and give you a reference. Let's take a look with the editor2016-12-12