如何使用CopyToDataTable()在C#中复制DateTimeMode? [英] How can I copy DateTimeMode in C# with CopyToDataTable()?

查看:310
本文介绍了如何使用CopyToDataTable()在C#中复制DateTimeMode?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用CopyToDataTable选择更大表的子集。但是, utcDT的DateTimeMode不会复制到新表中:

I am using CopyToDataTable to select a subset of a bigger table. However, the DateTimeMode of "utcDT" is not copied to new table:

        var x = new DataTable();
        x.Columns.Add("utcDT", typeof(System.DateTime));
        x.Columns["utcDT"].DateTimeMode = DataSetDateTime.Utc;
        x.Columns.Add("Symbol", typeof(System.String));

        var newRow = x.NewRow();
        newRow.SetField("utcDT", DateTime.Now);
        newRow.SetField("Symbol","A");
        x.Rows.Add(newRow);
        Console.WriteLine(x.Columns["utcDT"].DateTimeMode.ToString());

        var y = x.AsEnumerable().Where(s => s.Field<string>("Symbol")=="A").CopyToDataTable();
        Console.WriteLine(y.Columns["utcDT"].DateTimeMode.ToString());

如何确保它是

推荐答案

发生了什么事情



CopyToDataTable 仅复制列的名称和类型。

What's happening

CopyToDataTable only copies the columns' names and types.

参考来源


// We do not copy the same properties that DataView.ToTable does.
// If user needs that functionality, use other CopyToDataTable overloads.
// The reasoning being, the IEnumerator<DataRow> can be sourced from
// different DataTable, so we just use the "Default" instead of resolving the difference.

foreach (DataColumn column in current.Table.Columns)
{
    table.Columns.Add(column.ColumnName, column.DataType);
}




解决方案



1。 AsDataView()。ToTable()



使用 AsDataView (请参见使用DataView(从LINQ到DataSet) ToTable()

var z = x.AsEnumerable()
         .Where(s => s.Field<string>("Symbol") == "A")
         .AsDataView()
         .ToTable();



2。 Clone()-> CopyToDataTable< T>(IEnumerable< T>,DataTable,LoadOption)



您可以使用< a href = https://docs.microsoft.com/zh-cn/dotnet/api/system.data.datatable.clone?view=netframework-4.8 rel = nofollow noreferrer>克隆其中 克隆DataTable的结构,包括所有DataTable架构和约束。',后跟 CopyToDataTable ,但这是一个采用目标表的版本。 / p>

2. Clone() -> CopyToDataTable<T>(IEnumerable<T>, DataTable, LoadOption)

You can use Clone which 'Clones the structure of the DataTable, including all DataTable schemas and constraints.' followed by CopyToDataTable, but the one version that takes the target table.

var z = x.Clone();
x.AsEnumerable()
   .Where(s => s.Field<string>("Symbol") == "A")
   .CopyToDataTable(z,LoadOption.OverwriteChanges);



测试



Test

(your dt definition here) 
Console.WriteLine(x.Columns["utcDT"].DateTimeMode);

var y = x.AsEnumerable()
        .Where(s => s.Field<string>("Symbol") == "A")
        .CopyToDataTable<DataRow>();
Console.WriteLine(y.Columns["utcDT"].DateTimeMode);

var z = x.AsEnumerable()
        .Where(s => s.Field<string>("Symbol") == "A")
        .AsDataView()
        .ToTable();
Console.WriteLine(z.Columns["utcDT"].DateTimeMode);

var a = x.Clone();
x.AsEnumerable()
        .Where(s => s.Field<string>("Symbol") == "A")
        .CopyToDataTable(a,LoadOption.OverwriteChanges);
Console.WriteLine(a.Columns["utcDT"].DateTimeMode);



输出



Output

Utc
UnspecifiedLocal
Utc
Utc

这篇关于如何使用CopyToDataTable()在C#中复制DateTimeMode?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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