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

C# 如何快速将DataTable转换为IList<;myType>;,c#,wpf,list,datatable,ienumerable,C#,Wpf,List,Datatable,Ienumerable,在我的示例中,我尝试使用给定的源项查看网格控件中的关键信息。在本例中,为了传递items源代码,我需要将C#中的DataTable转换为IList 我尝试了所有传统的转换方法,包括下面的代码,将DataTable转换为IList dt.AsEnumerable().Cast<object>().ToList() 最后,我想从语法上做以下几点 IList<myType> list = new IList<myType>; list.Add(new myType

在我的示例中,我尝试使用给定的源项查看网格控件中的关键信息。在本例中,为了传递items源代码,我需要将C#中的
DataTable
转换为
IList

我尝试了所有传统的转换方法,包括下面的代码,将
DataTable
转换为
IList

dt.AsEnumerable().Cast<object>().ToList()
最后,我想从语法上做以下几点

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});
IList列表=新IList;
添加(新的myType(){PlanRowPID=“PID 0”,PlanRowClass=“CLASS 8”,PlanRowVendor=“VENDOR A”,PlanRowColor=COLOR D});
添加(新的myType(){PlanRowPID=“PID 1”,PlanRowClass=“CLASS 7”,PlanRowVendor=“VENDOR B”,PlanRowColor=COLOR C});
添加(新的myType(){PlanRowPID=“PID 2”,PlanRowClass=“CLASS 9”,PlanRowVendor=“VENDOR C”,PlanRowColor=COLOR B});
在我的应用程序中,我有10000多个数据列,因此不可能将数据表中的每个对象都添加到列表中。我需要花费更多的时间和知识来查找
DataTable
中可用的每个列的数据类型,然后根据它们的类型为
myType
类创建属性

因此,请提供将
数据表
转换为
IList

的最佳和最快捷的方法,请尝试

dt.AsEnumerable().Cast<object>().ToList()
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;  
}
私有静态列表转换数据表(数据表dt)
{  
列表数据=新列表();
foreach(数据行中的数据行)
{  
T项=获取项(行);
数据。添加(项目);
}  
返回数据;
} 
私有静态T GetItem(DataRow dr)
{  
温度类型=温度类型(T);
T obj=Activator.CreateInstance();
foreach(dr.Table.Columns中的DataColumn列)
{  
foreach(temp.GetProperties()中的PropertyInfo pro)
{  
if(pro.Name==column.ColumnName)
pro.SetValue(obj,dr[column.ColumnName],null);
其他的
继续;
}  
}  
返回obj;
}
要调用的示例

List<Student> studentDetails = new List<Student>();  
studentDetails = ConvertDataTable<Student>(dt);  
List studentDetails=new List();
studentDetails=ConvertDataTable(dt);
资料来源:

在本例中,为了传递items源代码,我需要将C#中的
DataTable
转换为
IList

您不能将
项资源
属性设置为
数据表的
默认视图

dataGrid.ItemsSource = dt.DefaultView;
那么就不需要将其转换为列表


DataView
implements
IList

您是否尝试过使用类似ORM的实体框架?是的,但只有在实体数量较少的情况下,ORM才有用。如果我有更多的实体,很难通过查找每列的数据类型来创建类。我需要编程的方法来找到类型并高效地创建类。你能告诉我们为什么有10000列吗?谢谢。但我的实际问题是,如何通过初始化从每个数据表行获得的属性来定义类“Student”。在初始化数据表中可用的属性时,定义类是否存在任何合乎语法的方法?。在我的例子中,我需要创建一个带有“PlanRowPID”、“PlanRowClass”等的类“Plan”,并按专业语法进行。
dt.AsEnumerable().Cast<object>().ToList()