我如何把一个列表与LT; T>到DataSet? [英] How do I transform a List<T> into a DataSet?

查看:104
本文介绍了我如何把一个列表与LT; T>到DataSet?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定对象的列表,我需要将其改造成一个数据集,其中列表中的每个项目再由行psented $ P $和每个属性是行中的一列。然后,该数据集将传递到<一个href=\"http://www.aspose.com/categories/file-format-components/aspose.cells-for-.net-and-java/default.aspx\">Aspose.Cells功能,以创建一个Excel文档作为报告。

Given a list of objects, I am needing to transform it into a dataset where each item in the list is represented by a row and each property is a column in the row. This DataSet will then be passed to an Aspose.Cells function in order to create an Excel document as a report.

说我有以下几点:

public class Record
{
   public int ID { get; set; }
   public bool Status { get; set; }
   public string Message { get; set; }
}

给出一个列表记录,我怎么把它改造成一个DataSet如下:

Given a List records, how do I transform it into a DataSet as follows:

ID Status Message
1  true   "message" 
2  false  "message2" 
3  true   "message3" 
...

目前,我能想到的是如下的唯一的事:

At the moment the only thing I can think of is as follows:

DataSet ds = new DataSet
ds.Tables.Add();
ds.Tables[0].Add("ID", typeof(int));    
ds.Tables[0].Add("Status", typeof(bool));
ds.Tables[0].Add("Message", typeof(string));

foreach(Record record in records)
{
    ds.Tables[0].Rows.Add(record.ID, record.Status, record.Message);
}

不过,这种方式让我的思维,必须有一个更好的办法,因为最起码,如果新的属性添加到记录那么他们将不会在DataSet中出现......但在同一时间,它可以让我控制的顺序每个属性被添加到行

But this way leaves me thinking there must be a better way since at the very least if new properties are added to Record then they won't show up in the DataSet...but at the same time it allows me to control the order each property is added to the row.

有谁知道一个更好的方式来做到这一点?

Does anyone know of a better way to do this?

推荐答案

您可以做到这一点通过反射和仿制药,检查的基本类型的属性。

You can do it through reflection and generics, inspecting the properties of the underlying type.

考虑一下我使用这个扩展方法:

Consider this extension method that I use:

    public static DataTable ToDataTable<T>(this IEnumerable<T> collection)
    {
        DataTable dt = new DataTable("DataTable");
        Type t = typeof(T);
        PropertyInfo[] pia = t.GetProperties();

        //Inspect the properties and create the columns in the DataTable
        foreach (PropertyInfo pi in pia)
        {
            Type ColumnType = pi.PropertyType;
            if ((ColumnType.IsGenericType))
            {
                ColumnType = ColumnType.GetGenericArguments()[0];
            }
            dt.Columns.Add(pi.Name, ColumnType);
        }

        //Populate the data table
        foreach (T item in collection)
        {
            DataRow dr = dt.NewRow();
            dr.BeginEdit();
            foreach (PropertyInfo pi in pia)
            {
                if (pi.GetValue(item, null) != null)
                {
                    dr[pi.Name] = pi.GetValue(item, null);
                }
            }
            dr.EndEdit();
            dt.Rows.Add(dr);
        }
        return dt;
    }

这篇关于我如何把一个列表与LT; T&GT;到DataSet?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆