简体   繁体   English

如何快速将DataTable转换为IList <myType>

[英]How do I quickly convert DataTable to IList<myType>

In my sample, i tried to view the pivotal information in Grid control with given items source. 在我的示例中,我尝试查看具有给定项源的Grid控件中的关键信息。 In this case to pass the items source, i need to convert the DataTable into IList<myType> in C#. 在这种情况下,要传递项目源,我需要将DataTable转换为C#中的IList<myType>

I have tried all the traditional conversion methods including the below code to convert the DataTable into IList . 我尝试了所有传统的转换方法,包括下面的代码,将DataTable转换为IList

dt.AsEnumerable().Cast<object>().ToList()

But the all the traditional methods are not helpful for me since, i don't know exactly what are the objects are used and their types in respective columns in DataTable . 但是所有传统方法对我都无济于事,因为我不知道确切使用了哪些对象以及它们在DataTable相应列中的类型。 Also the DataTable having more number of columns and rows. 另外, DataTable具有更多的列和行数。

For example, I am having the below column values in my data table. 例如,我的数据表中包含以下列值。

PlanRowPID,PlanRowClass,PlanRowVendor,PlanRowColor
PID 0,CLASS 8,VENDOR A,COLOR D
PID 1,CLASS 7,VENDOR B,COLOR C
PID 2,CLASS 9,VENDOR C,COLOR B
PID 3,CLASS 6,VENDOR D,COLOR B

And finally i want to do the following pro-grammatically, 最后,我想在语法上做以下事情,

IList<myType> list = new IList<myType>;
list.Add(new myType() {PlanRowPID = "PID 0",PlanRowClass = "CLASS 8",PlanRowVendor = "VENDOR A",PlanRowColor=COLOR D});
list.Add(new myType() {PlanRowPID = "PID 1",PlanRowClass = "CLASS 7",PlanRowVendor = "VENDOR B",PlanRowColor=COLOR C});
list.Add(new myType() {PlanRowPID = "PID 2",PlanRowClass = "CLASS 9",PlanRowVendor = "VENDOR C",PlanRowColor=COLOR B});

In my application I have more than 10000 data columns, so it is not possible to add each and every object from data table into list.I need to spend more time and knowledge to find the data type of each columns available in DataTable and then create the properties based on their types for myType class. 在我的应用程序中,我有超过10000个数据列,因此不可能将数据表中的每个对象都添加到列表中。我需要花费更多的时间和知识来查找DataTable可用的每个列的数据类型,然后创建属性基于myType类的类型。

So please provide any optimal and quickest way to convert the DataTable into IList<myType> . 因此,请提供任何最佳,最快的方法将DataTable转换为IList<myType>

Please try, 请试试,

private static List<T> ConvertDataTable<T>(DataTable dt)  
{  
    List<T> data = new List<T>();  
    foreach (DataRow row in dt.Rows)  
    {  
        T item = GetItem<T>(row);  
        data.Add(item);  
    }  
    return data;  
} 

private static T GetItem<T>(DataRow dr)  
{  
    Type temp = typeof(T);  
    T obj = Activator.CreateInstance<T>();  

    foreach (DataColumn column in dr.Table.Columns)  
    {  
        foreach (PropertyInfo pro in temp.GetProperties())  
        {  
            if (pro.Name == column.ColumnName)  
                pro.SetValue(obj, dr[column.ColumnName], null);  
            else  
                continue;  
        }  
    }  
    return obj;  
}

Example to invoke, 调用示例

List<Student> studentDetails = new List<Student>();  
studentDetails = ConvertDataTable<Student>(dt);  

Source :- http://www.c-sharpcorner.com/UploadFile/ee01e6/different-way-to-convert-datatable-to-list/ 来源:-http: //www.c-sharpcorner.com/UploadFile/ee01e6/different-way-to-convert-datatable-to-list/

In this case to pass the items source, i need to convert the DataTable into IList in C#. 在这种情况下,要传递项目源,我需要将DataTable转换为C#中的IList

Can't you just set the ItemsSource property to the DefaultView of the DataTable ? 您不能仅将ItemsSource属性设置为DataTableDefaultView吗?

dataGrid.ItemsSource = dt.DefaultView;

Then there is no need to convert it to a list. 然后,无需将其转换为列表。

DataView implements IList . DataView实现IList

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

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