在 C# 中遍历泛型类型列表
Iterate through Generic Typed List in c#
我正在尝试遍历通用类型对象列表,我能够获取对象的属性,但无法从对象的每个实例的属性中获取值。这是我的代码的样子:我想创建一个函数来转换传递给它的任何列表并将其转换为 DataTable。
--数据对象
public class StudentDo
{
public int Id {get;set}
public string Name {get;set}
}
--通用数据访问对象
public DataTable ConvertListToDataTable(List<T> list, string tableName = "")
{
var type = typeof(T);
var properties = type.GetProperties().ToList();
DataTable dt = new DataTable(tableName);
properties.ForEach(x =>
{
dt.Columns.Add(x.Name);
});
// i don't know how shall i pull data from each instance of List<T>.
return dt;
}
遍历列表并使用反射对每一列进行插入 -
public static DataTable ConvertListToDataTable<T>(List<T> list, string tableName = "")
{
var type = typeof(T);
var properties = type.GetProperties().ToList();
DataTable dt = new DataTable(tableName);
properties.ForEach(x =>
{
dt.Columns.Add(x.Name);
});
foreach (var item in list)
{
var dataRow = dt.NewRow();
properties.ForEach(x =>
{
dataRow[x.Name] = x.GetValue(item, null);
});
dt.Rows.Add(dataRow);
}
return dt;
}
这是我用的:
public DataTable ToDataTable<T>(IList<T> data)
{
PropertyDescriptorCollection properties =
TypeDescriptor.GetProperties(typeof(T));
DataTable table = new DataTable();
foreach (PropertyDescriptor prop in properties)
table.Columns.Add(prop.Name, Nullable.GetUnderlyingType(prop.PropertyType) ?? prop.PropertyType);
foreach (T item in data)
{
DataRow row = table.NewRow();
foreach (PropertyDescriptor prop in properties)
row[prop.Name] = prop.GetValue(item) ?? DBNull.Value;
table.Rows.Add(row);
}
return table;
}
我正在尝试遍历通用类型对象列表,我能够获取对象的属性,但无法从对象的每个实例的属性中获取值。这是我的代码的样子:我想创建一个函数来转换传递给它的任何列表并将其转换为 DataTable。
--数据对象
public class StudentDo
{
public int Id {get;set}
public string Name {get;set}
}
--通用数据访问对象
public DataTable ConvertListToDataTable(List<T> list, string tableName = "")
{
var type = typeof(T);
var properties = type.GetProperties().ToList();
DataTable dt = new DataTable(tableName);
properties.ForEach(x =>
{
dt.Columns.Add(x.Name);
});
// i don't know how shall i pull data from each instance of List<T>.
return dt;
}
遍历列表并使用反射对每一列进行插入 -
public static DataTable ConvertListToDataTable<T>(List<T> list, string tableName = "")
{
var type = typeof(T);
var properties = type.GetProperties().ToList();
DataTable dt = new DataTable(tableName);
properties.ForEach(x =>
{
dt.Columns.Add(x.Name);
});
foreach (var item in list)
{
var dataRow = dt.NewRow();
properties.ForEach(x =>
{
dataRow[x.Name] = x.GetValue(item, null);
});
dt.Rows.Add(dataRow);
}
return dt;
}
这是我用的:
public DataTable ToDataTable<T>(IList<T> data)
{
PropertyDescriptorCollection properties =
TypeDescriptor.GetProperties(typeof(T));
DataTable table = new DataTable();
foreach (PropertyDescriptor prop in properties)
table.Columns.Add(prop.Name, Nullable.GetUnderlyingType(prop.PropertyType) ?? prop.PropertyType);
foreach (T item in data)
{
DataRow row = table.NewRow();
foreach (PropertyDescriptor prop in properties)
row[prop.Name] = prop.GetValue(item) ?? DBNull.Value;
table.Rows.Add(row);
}
return table;
}