gpt4 book ai didi

c# - 我可以使用 LINQ 将 List 转换为 DataSet 吗?

转载 作者:太空宇宙 更新时间:2023-11-03 19:37:57 29 4
gpt4 key购买 nike

我对 C#/.NET 中的 LINQ 完全陌生。我知道我可以使用它将数据集转换为数组/列表,我可以朝相反的方向前进吗?

我正在使用 NPlot 生成捕获的价格图,这些价格存储在一个列表中,其中 PriceInformation 是一个包含两个公共(public) double 值和一个 DateTime 的类。

非常欢迎任何建议。

最佳答案

有一个方法叫做 CopyToDataTable .该方法仅在您已经拥有 IEnumerable(DataRow) 时才有用

这是我的做法:

//extension method to convert my type to an object array.
public static object[] ToObjectArray(this MyClass theSource)
{
object[] result = new object[3];
result[0] = theSource.FirstDouble;
result[1] = theSource.SecondDouble;
result[2] = theSource.TheDateTime;

return result;
}


//some time later, new up a dataTable, set it's columns, and then...

DataTable myTable = new DataTable()

DataColumn column1 = new DataColumn();
column1.DataType = GetType("System.Double");
column1.ColumnName = "FirstDouble";
myTable.Add(column1);

DataColumn column2 = new DataColumn();
column2.DataType = GetType("System.Double");
column2.ColumnName = "SecondDouble";
myTable.Add(column2);

DataColumn column3 = new DataColumn();
column3.DataType = GetType("System.DateTime");
column3.ColumnName = "TheDateTime";
myTable.Add(column3);

// ... Each Element becomes an array, and then a row
MyClassList.ForEach(x => myTable.Rows.Add(x.ToObjectArray());

关于c# - 我可以使用 LINQ 将 List<MyObjectType> 转换为 DataSet 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/219808/

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