2020-10-21

list转datatable(支持匿名类型)

 /// <summary>  /// List转成DataTable  /// </summary>  /// <typeparam name="T"></typeparam>  /// <param name="varlist"></param>  /// <returns></returns>  public static DataTable ListToDataTable<T>(IEnumerable<T> varlist)  {   DataTable dtReturn = new DataTable();   PropertyInfo[] oProps = null;   if (varlist == null) return dtReturn;   foreach (T rec in varlist)   {    if (oProps == null)    {     oProps = rec.GetType().GetProperties();     foreach (PropertyInfo pi in oProps)     {      Type colType = pi.PropertyType;      if ((colType.IsGenericType) && (colType.GetGenericTypeDefinition()      == typeof(Nullable<>)))      {       colType = colType.GetGenericArguments()[0];      }      dtReturn.Columns.Add(new DataColumn(pi.Name, colType));     }    }    DataRow dr = dtReturn.NewRow();    foreach (PropertyInfo pi in oProps)    {     dr[pi.Name] = pi.GetValue(rec, null) == null ? DBNull.Value : pi.GetValue     (rec, null);    }    dtReturn.Rows.Add(dr);   }   return dtReturn;  }

应用举例:

 

原文转载:http://www.shaoqun.com/a/481422.html

eprice:https://www.ikjzd.com/w/1325

斑马物流:https://www.ikjzd.com/w/1316

孙琦:https://www.ikjzd.com/w/1638


///<summary>///List转成DataTable///</summary>///<typeparamname="T"></typeparam>///<paramname="varlist"></param>///<returns></returns>p
盘古集团:https://www.ikjzd.com/w/1448
etsy:https://www.ikjzd.com/w/169.html
珠海梦幻水城联系电话?梦幻水城指定地区优惠票在哪买?:http://tour.shaoqun.com/a/19244.html
亚马逊已成为高风险投资,这些平台红线碰不得:https://www.ikjzd.com/home/112577
美元贬值:美国喜闻乐见,跨境卖家有惊无喜:https://www.ikjzd.com/home/453

No comments:

Post a Comment