C#将数据表复制到新数据表

时间:2017-07-30 12:49:54

标签: c# datatable sum copy

我即将创建一个将从数据表中填充的chartview。数据表看起来像这样:

enter image description here 编辑:

这就是我尝试使用代码的方式:

 DataTable table = new DataTable();

            using (var da = new MySqlDataAdapter("SELECT * from Emner WHERE LastCallDato <> ('" + DBNull.Value.ToString() + "');", Varribles.ConString))
            {
                da.Fill(table);
            }


            IFormatProvider provider = CultureInfo.InvariantCulture;
            table.Columns.Add("Date", typeof(DateTime));
            table.Columns.Add("Status", typeof(string));

            /*
            table.Rows.Add(new object[] { DateTime.ParseExact("26-07-2017 16:45", "dd-MM-yyyy HH:mm", provider), "Callback" });
            */

            string[] uniqueStatus = table.AsEnumerable().Select(x => x.Field<string>("Status")).Distinct().ToArray();

            DataTable results = new DataTable();
            results.Columns.Add("Date", typeof(DateTime));
            foreach (string status in uniqueStatus)
            {
                results.Columns.Add(status, typeof(int));
            }

            var groups = table.AsEnumerable().GroupBy(x => x.Field<DateTime>("Date").Date).ToList();

            foreach (var group in groups)
            {
                DataRow newRow = results.Rows.Add();
                newRow["Date"] = group.Key;
                var colGroups = group.GroupBy(x => x.Field<string>("Status")).ToList();
                foreach (var colGroup in colGroups)
                {
                    newRow[colGroup.Key] = colGroup.Count();
                }
            }

我收到错误说:

System.InvalidCastException: DBNull.Value cannot be converted to the type of 'System.DateTime'. Use another type that can be null

此行上的错误触发:

                var groups = table.AsEnumerable().GroupBy(x => x.Field<DateTime>("Date").Date).ToList();

1 个答案:

答案 0 :(得分:1)

您需要一个数据透视表。见下面的代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Globalization;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {

            IFormatProvider provider = CultureInfo.InvariantCulture;

            DataTable table = new DataTable();
            table.Columns.Add("Date", typeof(DateTime));
            table.Columns.Add("Status", typeof(string));

            table.Rows.Add(new object[] { DateTime.ParseExact("26-07-2017 16:45", "dd-MM-yyyy HH:mm", provider), "Callback" });
            table.Rows.Add(new object[] { DateTime.ParseExact("26-07-2017 16:48", "dd-MM-yyyy HH:mm", provider), "Callback" });
            table.Rows.Add(new object[] { DateTime.ParseExact("26-07-2017 08:59", "dd-MM-yyyy HH:mm", provider), "Callback" });
            table.Rows.Add(new object[] { DateTime.ParseExact("28-07-2017 18:16", "dd-MM-yyyy HH:mm", provider), "Ingen Kontakt" });
            table.Rows.Add(new object[] { DateTime.ParseExact("26-07-2017 09:00", "dd-MM-yyyy HH:mm", provider), "Callback" });
            table.Rows.Add(new object[] { DateTime.ParseExact("26-07-2017 18:35", "dd-MM-yyyy HH:mm", provider), "Ja" });
            table.Rows.Add(new object[] { DateTime.ParseExact("26-07-2017 17:43", "dd-MM-yyyy HH:mm", provider), "Callback" });
            table.Rows.Add(new object[] { DateTime.ParseExact("26-07-2017 19:33", "dd-MM-yyyy HH:mm", provider), "Ja" });
            table.Rows.Add(new object[] { DateTime.ParseExact("28-07-2017 18:16", "dd-MM-yyyy HH:mm", provider), "Ingen Kontakt" });
            table.Rows.Add(new object[] { DateTime.ParseExact("28-07-2017 18:16", "dd-MM-yyyy HH:mm", provider), "Optaget" });
            table.Rows.Add(new object[] { DateTime.ParseExact("28-07-2017 18:16", "dd-MM-yyyy HH:mm", provider), "Optaget" });
            table.Rows.Add(new object[] { });

            string[] uniqueStatus = table.AsEnumerable().Select(x => x.Field<string>("Status")).Distinct().Where(x => x != null).ToArray();

            DataTable results = new DataTable();
            results.Columns.Add("Date", typeof(DateTime));
            foreach (string status in uniqueStatus)
            {
                results.Columns.Add(status, typeof(int));
            }

            var groups = table.AsEnumerable().Where(x => x.Field<object>("Date") != null).GroupBy(x => x.Field<DateTime>("Date").Date).ToList();

            foreach (var group in groups)
            {
                DataRow newRow = results.Rows.Add();
                newRow["Date"] = group.Key;
                var colGroups = group.GroupBy(x => x.Field<string>("Status")).ToList();
                foreach (var colGroup in colGroups)
                {
                    if (colGroup.Key.Length > 0)
                    {
                        newRow[colGroup.Key] = colGroup.Count();
                    }
                }
            }
        }
    }
}