我对 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());
我是一名优秀的程序员,十分优秀!