简体   繁体   中英

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. In this case to pass the items source, i need to convert the DataTable into IList<myType> in C#.

I have tried all the traditional conversion methods including the below code to convert the DataTable into 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 . Also the DataTable having more number of columns and rows.

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.

So please provide any optimal and quickest way to convert the DataTable into 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/

In this case to pass the items source, i need to convert the DataTable into IList in C#.

Can't you just set the ItemsSource property to the DefaultView of the DataTable ?

dataGrid.ItemsSource = dt.DefaultView;

Then there is no need to convert it to a list.

DataView implements IList .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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