如何将数组集合转换为数据集 C#
How to convert array collection to a dataset c#
您好,有一个回复 class 有三个模型,都是特定类型...
public class AssociationResponse
{
public AssociationModel ItemDetail {get;set;}
public AssociationModel[] Children {get;set;}
public AssociationModel Parent {get; set;}
public int ErrorCode { get; set; }
public string ErrorMessage { get; set; }
}
现在我正在努力研究如何将 AssociationModel[] Children 转换为简单的数据集,以便我可以将它们转换为数据表并在树图中显示结果..除非有人知道更好的方法?
在我的代码后面
public AssociationModel ItemDetail { get; set; } = new AssociationModel();
public AssociationModel Parent { get; set; } = new AssociationModel();
public AssociationModel Child { get; set; } = new AssociationModel();
public async Task LoadData(string SerialNumber)
{
try
{
GetAssociationBySerialNumberRequest request = new GetAssociationBySerialNumberRequest()
{
SerialNumber = SerialNumber
};
response = await IAssociation.AssociationGetData(request);
AssociationResponse resp = new AssociationResponse();
if (SerialNumber != null)
{
ItemDetail = response.ItemDetail;
Parent = response.Parent;
**DataSet dset = new DataSet();**
}
}
任何帮助都将不胜感激。 P.S 我的 [] Children 中有三个表。所以我想以某种方式访问它们,我尝试保存到数据表类型,但这不起作用。任何帮助表示赞赏。
编辑
我遇到的问题是我似乎无法将数组转换为数据集。不知道该怎么做。
这对你有用吗?
我使用这个扩展方法将 IEnumerable 转换为 Datatables
public static DataTable ToDataTable<T>(this IList<T> data)
{
var props = TypeDescriptor.GetProperties(typeof(T));
var table = new DataTable();
for (var i = 0; i < props.Count; i++)
{
var prop = props[i];
Type propertyType;
if (prop.PropertyType.IsGenericType &&
prop.PropertyType.GetGenericTypeDefinition() == typeof(Nullable<>))
{
propertyType = prop.PropertyType.GetGenericArguments()[0];
}
else
{
propertyType = prop.PropertyType;
}
table.Columns.Add(prop.Name, propertyType);
}
var values = new object[props.Count];
foreach (var item in data)
{
for (var i = 0; i < values.Length; i++)
{
values[i] = props[i].GetValue(item);
}
table.Rows.Add(values);
}
return table;
}
已编辑
这个方法怎么样?
public static DataTable ToDataTable(IList<AssociationModel> data)
{
var props = TypeDescriptor.GetProperties(typeof(AssociationModel));
var table = new DataTable();
for (var i = 0; i < props.Count; i++)
{
var prop = props[i];
Type propertyType;
if (prop.PropertyType.IsGenericType &&
prop.PropertyType.GetGenericTypeDefinition() == typeof(Nullable<>))
{
propertyType = prop.PropertyType.GetGenericArguments()[0];
}
else
{
propertyType = prop.PropertyType;
}
table.Columns.Add(prop.Name, propertyType);
}
var values = new object[props.Count];
foreach (var item in data)
{
for (var i = 0; i < values.Length; i++)
{
values[i] = props[i].GetValue(item);
}
table.Rows.Add(values);
}
return table;
}
您的代码将如下更改
if (SerialNumber != null)
{
ItemDetail = response.ItemDetail;
Parent = response.Parent;
DataTable dataTable = ToDataTable(response.Children.ToList());
}
您好,有一个回复 class 有三个模型,都是特定类型...
public class AssociationResponse
{
public AssociationModel ItemDetail {get;set;}
public AssociationModel[] Children {get;set;}
public AssociationModel Parent {get; set;}
public int ErrorCode { get; set; }
public string ErrorMessage { get; set; }
}
现在我正在努力研究如何将 AssociationModel[] Children 转换为简单的数据集,以便我可以将它们转换为数据表并在树图中显示结果..除非有人知道更好的方法?
在我的代码后面
public AssociationModel ItemDetail { get; set; } = new AssociationModel();
public AssociationModel Parent { get; set; } = new AssociationModel();
public AssociationModel Child { get; set; } = new AssociationModel();
public async Task LoadData(string SerialNumber)
{
try
{
GetAssociationBySerialNumberRequest request = new GetAssociationBySerialNumberRequest()
{
SerialNumber = SerialNumber
};
response = await IAssociation.AssociationGetData(request);
AssociationResponse resp = new AssociationResponse();
if (SerialNumber != null)
{
ItemDetail = response.ItemDetail;
Parent = response.Parent;
**DataSet dset = new DataSet();**
}
}
任何帮助都将不胜感激。 P.S 我的 [] Children 中有三个表。所以我想以某种方式访问它们,我尝试保存到数据表类型,但这不起作用。任何帮助表示赞赏。
编辑
我遇到的问题是我似乎无法将数组转换为数据集。不知道该怎么做。
这对你有用吗?
我使用这个扩展方法将 IEnumerable 转换为 Datatables
public static DataTable ToDataTable<T>(this IList<T> data)
{
var props = TypeDescriptor.GetProperties(typeof(T));
var table = new DataTable();
for (var i = 0; i < props.Count; i++)
{
var prop = props[i];
Type propertyType;
if (prop.PropertyType.IsGenericType &&
prop.PropertyType.GetGenericTypeDefinition() == typeof(Nullable<>))
{
propertyType = prop.PropertyType.GetGenericArguments()[0];
}
else
{
propertyType = prop.PropertyType;
}
table.Columns.Add(prop.Name, propertyType);
}
var values = new object[props.Count];
foreach (var item in data)
{
for (var i = 0; i < values.Length; i++)
{
values[i] = props[i].GetValue(item);
}
table.Rows.Add(values);
}
return table;
}
已编辑
这个方法怎么样?
public static DataTable ToDataTable(IList<AssociationModel> data)
{
var props = TypeDescriptor.GetProperties(typeof(AssociationModel));
var table = new DataTable();
for (var i = 0; i < props.Count; i++)
{
var prop = props[i];
Type propertyType;
if (prop.PropertyType.IsGenericType &&
prop.PropertyType.GetGenericTypeDefinition() == typeof(Nullable<>))
{
propertyType = prop.PropertyType.GetGenericArguments()[0];
}
else
{
propertyType = prop.PropertyType;
}
table.Columns.Add(prop.Name, propertyType);
}
var values = new object[props.Count];
foreach (var item in data)
{
for (var i = 0; i < values.Length; i++)
{
values[i] = props[i].GetValue(item);
}
table.Rows.Add(values);
}
return table;
}
您的代码将如下更改
if (SerialNumber != null)
{
ItemDetail = response.ItemDetail;
Parent = response.Parent;
DataTable dataTable = ToDataTable(response.Children.ToList());
}