zoukankan      html  css  js  c++  java
  • list<T>转DataTable

    View Code
    1 /// <summary>
    2 /// 将集合类转换成DataTable
    3 /// </summary>
    4 /// <param name="list">集合</param>
    5 /// <returns></returns>
    6 public static DataTable ToDataTable(IList list)
    7 {
    8 DataTable result = new DataTable();
    9 if (list.Count > 0)
    10 {
    11 PropertyInfo[] propertys = list[0].GetType().GetProperties();
    12 foreach (PropertyInfo pi in propertys)
    13 {
    14 result.Columns.Add(pi.Name, pi.PropertyType);
    15 }
    16
    17 for (int i = 0; i < list.Count; i++)
    18 {
    19 ArrayList tempList = new ArrayList();
    20 foreach (PropertyInfo pi in propertys)
    21 {
    22 object obj = pi.GetValue(list[i], null);
    23 tempList.Add(obj);
    24 }
    25 object[] array = tempList.ToArray();
    26 result.LoadDataRow(array, true);
    27 }
    28 }
    29 return result;
    30 }
    31
    32 /// <summary>
    33 /// 将泛型集合类转换成DataTable
    34 /// </summary>
    35 /// <typeparam name="T">集合项类型</typeparam>
    36 /// <param name="list">集合</param>
    37 /// <returns>数据集(表)</returns>
    38 public static DataTable ToDataTable<T>(IList<T> list)
    39 {
    40 return ConvertX.ToDataTable<T>(list, null);
    41 }
    42
    43 /// <summary>
    44 /// 将泛型集合类转换成DataTable
    45 /// </summary>
    46 /// <typeparam name="T">集合项类型</typeparam>
    47 /// <param name="list">集合</param>
    48 /// <param name="propertyName">需要返回的列的列名</param>
    49 /// <returns>数据集(表)</returns>
    50 public static DataTable ToDataTable<T>(IList<T> list, params string[] propertyName)
    51 {
    52 List<string> propertyNameList = new List<string>();
    53 if (propertyName != null)
    54 propertyNameList.AddRange(propertyName);
    55
    56 DataTable result = new DataTable();
    57 if (list.Count > 0)
    58 {
    59 PropertyInfo[] propertys = list[0].GetType().GetProperties();
    60 foreach (PropertyInfo pi in propertys)
    61 {
    62 if (propertyNameList.Count == 0)
    63 {
    64 result.Columns.Add(pi.Name, pi.PropertyType);
    65 }
    66 else
    67 {
    68 if (propertyNameList.Contains(pi.Name))
    69 result.Columns.Add(pi.Name, pi.PropertyType);
    70 }
    71 }
    72
    73 for (int i = 0; i < list.Count; i++)
    74 {
    75 ArrayList tempList = new ArrayList();
    76 foreach (PropertyInfo pi in propertys)
    77 {
    78 if (propertyNameList.Count == 0)
    79 {
    80 object obj = pi.GetValue(list[i], null);
    81 tempList.Add(obj);
    82 }
    83 else
    84 {
    85 if (propertyNameList.Contains(pi.Name))
    86 {
    87 object obj = pi.GetValue(list[i], null);
    88 tempList.Add(obj);
    89 }
    90 }
    91 }
    92 object[] array = tempList.ToArray();
    93 result.LoadDataRow(array, true);
    94 }
    95 }
    96 return result;
    97 }
  • 相关阅读:
    从关系型数据库到非关系型数据库
    2016某知名互联网公司PHP面试题及答案
    企业网站核心关键词如何去选择
    写Seo网站标题应该注意什么
    什么样的外链才是优质外链
    什么是网站物理链接结构
    需要分析竞争对手的网站哪些SEO数据
    做外链的时候应该需要注意什么
    描述标签对关键词排名有影响吗
    网站外链对排名的影响有哪些
  • 原文地址:https://www.cnblogs.com/fuhaidasheng/p/2070076.html
Copyright © 2011-2022 走看看