我可以使用LINQ来转换List< MyObjectType>进入数据集? [英] Can I use LINQ to convert a List<MyObjectType> into a DataSet?

查看:39
本文介绍了我可以使用LINQ来转换List< MyObjectType>进入数据集?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对C#/.NET中的LINQ完全陌生.我知道我可以使用它将DataSet转换为Array/List,我可以朝相反的方向走吗?

I am completely new to LINQ in C#/.NET. I understand that I could use it to convert a DataSet into an Array/List, am I able to go in the opposite direction?

我正在使用NPlot生成捕获的价格的图形,这些图形存储在一个List中,其中PriceInformation是一个包含两个公共双打和一个DateTime的类.

I'm using NPlot to generate a graph of captured prices, which are stored in a List, where PriceInformation is a class containing two public doubles and a DateTime.

任何建议都非常欢迎.

推荐答案

有一种称为

There's a method called CopyToDataTable. That method will only help if you already have a 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());

这篇关于我可以使用LINQ来转换List< MyObjectType>进入数据集?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆