简体   繁体   English

从多个不均匀列表创建数据表的最佳方法是什么 <T> ?

[英]What is the best way to create datatable from multiple uneven list<T>?

I have 4 lists of 'List' with different kind of data(text) and number of items in each list is also not same. 我有4个“列表”列表,它们具有不同类型的数据(文本),每个列表中的项数也不相同。 What would be the best way to create data table from those lists? 从这些列表创建数据表的最佳方法是什么?

Ex. 例如

class A {
  public int id {get; set;}
  public string Name {get; set;}
  public string detailInfo {get; set;}
}

class B {
  public string info {get; set;}
}

class C {
  public string Code {get; set;}
}

class D {
  public int contact {get; set;}
  public int contact2 {get; set;}
  public Addr {get; set;}
}

class E {
  main() {
    IList<A> listA= new List<A>();
    IList<B> listB= new List<B>();
    IList<C> listC= new List<C>();
    IList<D> listC= new List<D>();
  }
}

So from above 4 lists what will be the best way to create data table? 因此,从上面的4个列表中可以看出,创建数据表的最佳方法是什么? Note:Number of items in each list may vary.. 注意:每个列表中的项目数可能会有所不同。

I'd define the column names with reflection and just iterate over the records in your list to populate the rows. 我将使用反射定义列名称,然后遍历列表中的记录以填充行。 Something like this: 像这样:

public DataTable GetDataTable<T>(List<T> list) where T : class
{
   DataTable table = new DataTable();
   var fields = typeof(T).GetFields();

   //Create Columns
   foreach(var field in fields)
   {
      DataColumn c = new DataColumn(field.Name, field.GetType());
      c.AllowDbNull = true;
      table.Columns.Add(c);
   }

   //Create rows
   foreach(T record in list)
   {
      DataRow row = table.NewRow();

      foreach(var field in fields)
      {
         //If it's null the cell will contain DbNull.Value
         if(field.GetValue(record) != null)
            row[field.Name] = field.GetValue(record);
      }

      table.Rows.Add(row);
   }

   return table;
}

This will give you a datatable with as columnNames the propertynames of your classes and as rows the values of each record in the list. 这将为您提供一个数据表,其类的属性名称为columnNames,列表中各记录的值作为行。 If one of your properties is null, the datatable will contain DbNull.Value . 如果您的属性之一为null,则数据表将包含DbNull.Value

@Alexander I managed to populate required table using below code: @Alexander我设法使用以下代码填充所需的表:

public static DataTable ToDataTable<T>(List<T> items)
{
        DataTable dataTable = new DataTable(typeof(T).Name);

        //Get all the properties
        PropertyInfo[] Props = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance)`enter code here`;
        foreach (PropertyInfo prop in Props)
        {
            //Setting column names as Property names
            dataTable.Columns.Add(prop.Name);
        }
        foreach (T item in items)
        {
           var values = new object[Props.Length];
           for (int i = 0; i < Props.Length; i++)
           {
                //inserting property values to datatable rows
                values[i] = Props[i].GetValue(item, null);
           }
           dataTable.Rows.Add(values);
      }
      return dataTable;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM