gpt4 book ai didi

c# - 如何在 C# 中将 IEnumerable 列表转换为数据表

转载 作者:行者123 更新时间:2023-12-04 17:59:55 25 4
gpt4 key购买 nike

我遇到了以下问题:

我通常使用下面的函数将自定义数据传输到 DataTable,但在这种情况下,输入数据是由列表组成的类。我只需要在 DataTable 中写入每行每个列表的第一个元素。你能帮我调整一下这个功能来实现吗?

public List<int> xID { get; set; }
public List<string> xName { get; set; }
public List<string> xType { get; set; }
public List<string> xSource { get; set; }
public List<int> xmdID { get; set; }
public List<string> xMDName { get; set; }
public List<string> xUser { get; set; }

public static DataTable ListToDataTable<T>(IEnumerable<T> list)
{
Type type = typeof(T);
var properties = type.GetProperties();

DataTable dataTable = new DataTable();
foreach (PropertyInfo info in properties)
{
dataTable.Columns.Add(new DataColumn(info.Name, info.PropertyType));
}

foreach (T entity in list)
{
object[] values = new object[properties.Length];
for (int i = 0; i < properties.Length; i++)
{
values[i] = properties[i].GetValue(entity);
}

dataTable.Rows.Add(values);
}

return dataTable;
}

根据 Carra 的回答,我尝试了下面的代码,但它无法识别 pType 类型(找不到类型或命名空间名称“pType”(您是否缺少 using 指令或程序集引用?))

var v = properties[i].GetValue(entity);
Type pType = properties[i].PropertyType;

if (pType.IsGenericType && pType.GetGenericTypeDefinition() == typeof(Nullable<>))
pType = Nullable.GetUnderlyingType(pType);

if (pType.IsEnum)
pType = Enum.GetUnderlyingType(pType);

if (v.GetType().GetGenericTypeDefinition() == typeof(List<>))
{
values[i] = (v as List<pType>).First();
}

更新

我想这不是最好的解决方案,因为它不接受任何列表类型,但这是我能做的最好的解决方案:

    public static DataTable ListToDataTable<T>(IEnumerable<T> list)
{
Type type = typeof(T);
var properties = type.GetProperties();

DataTable dataTable = new DataTable();
foreach (PropertyInfo info in properties)
{
Type propertyType = info.PropertyType;

if (propertyType.IsGenericType | propertyType.GetGenericTypeDefinition() == typeof(Nullable<>) | propertyType.IsEnum)
propertyType = propertyType.GetGenericArguments()[0];

dataTable.Columns.Add(new DataColumn(info.Name, propertyType));
}

foreach (T entity in list)
{
object[] values = new object[properties.Length];
for (int i = 0; i < properties.Length; i++)
{
var v = properties[i].GetValue(entity);
Type pType = v.GetType().GetGenericArguments().FirstOrDefault();

if (v.GetType().GetGenericTypeDefinition() == typeof(List<>))
{
if(pType == typeof(int))
values[i] = (v as List<int>).First();
else if(pType == typeof(string))
values[i] = (v as List<string>).First();
}
else
{
values[i] = v;
}
}
dataTable.Rows.Add(values);
}
return dataTable;
}

最佳答案

你需要这样的东西:

foreach (PropertyInfo info in properties)
{
if(info.PropertyType == typeof(IEnumerable))
{
dataTable.Columns.Add(new DataColumn(info.Name, info.Cast<object>().First().GetType());
}
else
{
dataTable.Columns.Add(new DataColumn(info.Name, info.PropertyType));
}
}

foreach (T entity in list)
{
object[] values = new object[properties.Length];
for (int i = 0; i < properties.Length; i++)
{
var v = properties[i].GetValue(entity);
if(v is IEnumerable)
{
values[i] = (v.Cast<object>().First()).First();
}
else
{
values[i] = v;
}
}

dataTable.Rows.Add(values);
}

关于c# - 如何在 C# 中将 IEnumerable<T> 列表转换为数据表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36735124/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com