欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C# DataTable與Model互轉(zhuǎn)的示例代碼

 更新時(shí)間:2020年12月04日 14:20:47   作者:李志強(qiáng)  
這篇文章主要介紹了C#DataTable與Model互轉(zhuǎn)的示例代碼,幫助大家更好的理解和使用c#,感興趣的朋友可以了解下
/// <summary>
 /// 實(shí)體轉(zhuǎn)換輔助類
 /// </summary>
 public class ModelConvertHelper<T> where T : new()
 {
  /// <summary>
  /// List泛型轉(zhuǎn)換DataTable.
  /// </summary>
  public DataTable ListToDataTable<T>(List<T> items)
  {
   var tb = new DataTable(typeof(T).Name);

   PropertyInfo[] props = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);

   foreach (PropertyInfo prop in props)
   {
    Type t = GetCoreType(prop.PropertyType);
    tb.Columns.Add(prop.Name, t);
   }

   foreach (T item in items)
   {
    var values = new object[props.Length];

    for (int i = 0; i < props.Length; i++)
    {
     values[i] = props[i].GetValue(item, null);
    }

    tb.Rows.Add(values);
   }

   return tb;
  }

  /// <summary>
  /// model轉(zhuǎn)換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(BindingFlags.Public | BindingFlags.Instance);

   foreach (PropertyInfo prop in props)
   {
    Type t = GetCoreType(prop.PropertyType);
    tb.Columns.Add(prop.Name, t);
   }


   var values = new object[props.Length];

   for (int i = 0; i < props.Length; i++)
   {
    values[i] = props[i].GetValue(items, null);
   }

   tb.Rows.Add(values);


   return tb;
  }

  /// <summary>
  /// Determine of specified type is nullable
  /// </summary>
  public static bool IsNullable(Type t)
  {
   return !t.IsValueType || (t.IsGenericType && t.GetGenericTypeDefinition() == 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 (!t.IsValueType)
    {
     return t;
    }
    else
    {
     return Nullable.GetUnderlyingType(t);
    }
   }
   else
   {
    return t;
   }
  }

  /// <summary>
  /// DataTable轉(zhuǎn)換泛型List
  /// </summary>
  /// <param name="dt"></param>
  /// <returns></returns>
  public static List<T> DataTableToList(DataTable dt)
  {
   // 定義集合
   List<T> ts = new List<T>();

   // 獲得此模型的類型
   Type type = typeof(T);
   string tempName = "";
   foreach (DataRow dr in dt.Rows)
   {
    T t = new T();
    // 獲得此模型的公共屬性
    PropertyInfo[] propertys = t.GetType().GetProperties();
    foreach (PropertyInfo pi in propertys)
    {
     tempName = pi.Name; // 檢查DataTable是否包含此列

     if (dt.Columns.Contains(tempName))
     {
      // 判斷此屬性是否有Setter
      if (!pi.CanWrite) continue;

      object value = dr[tempName];
      if (value != DBNull.Value)
       pi.SetValue(t, value, null);
     }
    }
    ts.Add(t);
   }
   return ts;
  }


  public static T DataTableToModel(DataTable dt)
  {
   // 定義實(shí)體
   T t = new T();

   // 獲得此模型的類型
   Type type = typeof(T);
   string tempName = "";

   foreach (DataRow dr in dt.Rows)
   {

    // 獲得此模型的公共屬性
    PropertyInfo[] propertys = t.GetType().GetProperties();
    foreach (PropertyInfo pi in propertys)
    {
     tempName = pi.Name; // 檢查DataTable是否包含此列

     if (dt.Columns.Contains(tempName))
     {
      // 判斷此屬性是否有Setter
      if (!pi.CanWrite) continue;

      object value = dr[tempName];
      if (value != DBNull.Value)
       pi.SetValue(t, value, null);
     }
    }
    break;
   }
   return t;
  }
 }

以上就是C#DataTable與Model互轉(zhuǎn)的示例代碼的詳細(xì)內(nèi)容,更多關(guān)于C#DataTable與Model互轉(zhuǎn)的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • C# 實(shí)現(xiàn)繪制PDF嵌套表格案例詳解

    C# 實(shí)現(xiàn)繪制PDF嵌套表格案例詳解

    嵌套表格,顧名思義,就是在一張表格中的特定單元格中再插入一個(gè)或者多個(gè)表格,本文將為大家介紹C#繪制PDF嵌套表格的代碼示例,需要的同學(xué)可以參考一下
    2021-11-11
  • C#實(shí)現(xiàn)目錄跳轉(zhuǎn)(TreeView和SplitContainer)的示例代碼

    C#實(shí)現(xiàn)目錄跳轉(zhuǎn)(TreeView和SplitContainer)的示例代碼

    本文主要介紹了C#實(shí)現(xiàn)目錄跳轉(zhuǎn)(TreeView和SplitContainer)的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-07-07
  • C#實(shí)現(xiàn)求一組數(shù)據(jù)眾數(shù)的方法

    C#實(shí)現(xiàn)求一組數(shù)據(jù)眾數(shù)的方法

    這篇文章主要介紹了C#實(shí)現(xiàn)求一組數(shù)據(jù)眾數(shù)的方法,這里以浮點(diǎn)型數(shù)組為例分析了C#求眾數(shù)的算法原理與實(shí)現(xiàn)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2015-08-08
  • c#之關(guān)閉窗體的實(shí)現(xiàn)方法

    c#之關(guān)閉窗體的實(shí)現(xiàn)方法

    這篇文章主要介紹了c#之關(guān)閉窗體的實(shí)現(xiàn)方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-07-07
  • C# 實(shí)現(xiàn)顏色的梯度漸變案例

    C# 實(shí)現(xiàn)顏色的梯度漸變案例

    這篇文章主要介紹了C# 實(shí)現(xiàn)顏色的梯度漸變案例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2021-01-01
  • c# 代理模式

    c# 代理模式

    代理模式:為其他對(duì)象提供一種代理以控制其他對(duì)象的訪問
    2012-10-10
  • C# 字符串與unicode互相轉(zhuǎn)換實(shí)戰(zhàn)案例

    C# 字符串與unicode互相轉(zhuǎn)換實(shí)戰(zhàn)案例

    這篇文章主要介紹了C# 字符串與unicode互相轉(zhuǎn)換實(shí)戰(zhàn)案例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2021-01-01
  • winform去掉右上角關(guān)閉按鈕的方法

    winform去掉右上角關(guān)閉按鈕的方法

    這篇文章主要介紹了winform去掉右上角關(guān)閉按鈕的方法,需要的朋友可以參考下
    2014-02-02
  • unity3D實(shí)現(xiàn)攝像機(jī)抖動(dòng)特效

    unity3D實(shí)現(xiàn)攝像機(jī)抖動(dòng)特效

    這篇文章主要為大家詳細(xì)介紹了unity3D實(shí)現(xiàn)攝像機(jī)抖動(dòng)特效,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-01-01
  • 詳解C# Socket異步通信實(shí)例

    詳解C# Socket異步通信實(shí)例

    本篇文章主要介紹了C# Socket異步通信,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2016-12-12

最新評(píng)論