我对C#/ .NET中的LINQ完全不熟悉.我知道我可以使用它将DataSet转换为数组/列表,我能朝相反的方向前进吗?
我正在使用NPlot生成捕获价格的图表,这些图表存储在List中,其中PriceInformation是一个包含两个公共双打和一个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());
Run Code Online (Sandbox Code Playgroud)