Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 我可以使用LINQ将列表转换为数据集吗?_C#_Linq - Fatal编程技术网

C# 我可以使用LINQ将列表转换为数据集吗?

C# 我可以使用LINQ将列表转换为数据集吗?,c#,linq,C#,Linq,我对C/.NET中的LINQ是完全陌生的。我知道我可以使用它将数据集转换为数组/列表,我是否可以使用相反的方向 我使用NPlot生成一个捕获价格的图表,它存储在一个列表中,其中PriceInformation是一个包含两个公共双精度和一个DateTime的类 欢迎任何建议。有一种方法叫做。只有当您已经有IEnumerableDataRow时,该方法才有帮助 我是这样做的: //extension method to convert my type to an object array. publ

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

我使用NPlot生成一个捕获价格的图表,它存储在一个列表中,其中PriceInformation是一个包含两个公共双精度和一个DateTime的类

欢迎任何建议。

有一种方法叫做。只有当您已经有IEnumerableDataRow时,该方法才有帮助

我是这样做的:

//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());
有一个方法叫做。只有当您已经有IEnumerableDataRow时,该方法才有帮助

我是这样做的:

//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());
如果MyObjectType是linq生成的实体,并且这些对象尚未关联到您可以调用的数据上下文

foreach( MyObjectType value in myList )
{
    dataContext.MyObkectTypes.InsertOnSubmit(value);
}
dataContext.SubmitChanges();
然而,目前LINQtoSQL在批量更新方面并不是非常有效。如果myList是1000个条目,您将有1000条insert语句

对于非常大的列表,您可以将列表转换为xml,并使用sql Server使用xml大容量插入的功能。您可以将sql server存储过程附加到datacontext

string xml = CreateInsertXml( myList );
dataContext.usp_MyObjectsBulkInsertXml(xml);
通过xml进行大容量插入的sql server存储过程示例

-- XML is expected in the following format:
--
--  <List>
--      <Item>
--          <PlotID>1234</PlotID>
--          <XValue>2.4</SmsNumber>     
--          <YValue>3.2</ContactID>
--          <ResultDate>12 Mar 2008</ResultDate>
--      </Item>
--      <Item>
--          <PlotID>3241</PlotID>
--          <XValue>1.4</SmsNumber>     
--          <YValue>5.2</ContactID>
--          <ResultDate>3 Mar 2008</ResultDate>
--      </Item>
--  </List>

CREATE PROCEDURE [dbo].usp_MyObjectsBulkInsertXml
(
    @MyXML XML
)
AS

DECLARE @DocHandle INT
EXEC sp_xml_preparedocument @DocHandle OUTPUT, @MyXML

INSERT INTO MyTable (
    PlotID,
    XValue,
    YValue,
    ResultDate
) 
SELECT
    X.PlotID,
    X.XValue,
    X.YValue,
    X.ResultDate
FROM OPENXML(@DocHandle, N'/List/Item', 2)
WITH (
    PlotID INT,
    XValue FLOAT,
    YValue FLOAT,
    ResultDate DATETIME
) X

EXEC sp_xml_removedocument @DocHandle

GO
如果MyObjectType是linq生成的实体,并且这些对象尚未关联到您可以调用的数据上下文

foreach( MyObjectType value in myList )
{
    dataContext.MyObkectTypes.InsertOnSubmit(value);
}
dataContext.SubmitChanges();
然而,目前LINQtoSQL在批量更新方面并不是非常有效。如果myList是1000个条目,您将有1000条insert语句

对于非常大的列表,您可以将列表转换为xml,并使用sql Server使用xml大容量插入的功能。您可以将sql server存储过程附加到datacontext

string xml = CreateInsertXml( myList );
dataContext.usp_MyObjectsBulkInsertXml(xml);
通过xml进行大容量插入的sql server存储过程示例

-- XML is expected in the following format:
--
--  <List>
--      <Item>
--          <PlotID>1234</PlotID>
--          <XValue>2.4</SmsNumber>     
--          <YValue>3.2</ContactID>
--          <ResultDate>12 Mar 2008</ResultDate>
--      </Item>
--      <Item>
--          <PlotID>3241</PlotID>
--          <XValue>1.4</SmsNumber>     
--          <YValue>5.2</ContactID>
--          <ResultDate>3 Mar 2008</ResultDate>
--      </Item>
--  </List>

CREATE PROCEDURE [dbo].usp_MyObjectsBulkInsertXml
(
    @MyXML XML
)
AS

DECLARE @DocHandle INT
EXEC sp_xml_preparedocument @DocHandle OUTPUT, @MyXML

INSERT INTO MyTable (
    PlotID,
    XValue,
    YValue,
    ResultDate
) 
SELECT
    X.PlotID,
    X.XValue,
    X.YValue,
    X.ResultDate
FROM OPENXML(@DocHandle, N'/List/Item', 2)
WITH (
    PlotID INT,
    XValue FLOAT,
    YValue FLOAT,
    ResultDate DATETIME
) X

EXEC sp_xml_removedocument @DocHandle

GO