SoFunction
Updated on 2025-03-07

C# DataTable and Model Transfer Example Code

C# DataTable and Model Transfer Example Code

Updated: December 4, 2020 14:20:47 Author: Li Zhiqiang
This article mainly introduces the sample code of C#DataTable and Model to help everyone better understand and use C#. Interested friends can learn about it.
/// <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!

  • C#
  • DataTable
  • Model

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-11
  • C# 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-07
  • C# 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-08
  • How 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 advice
    2023-07-07
  • C# 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 editor
    2021-01-01
  • C# proxy mode

    Proxy mode: Provides a proxy for other objects to control access to other objects
    2012-10-10
  • C# 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 editor
    2021-01-01
  • How 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 it
    2014-02-02
  • Unity3D 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-01
  • Detailed 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 editor
    2016-12-12

Latest Comments