我怎样才能解决'Invalidcastexception'

时间:2017-05-15 08:11:57

标签: c# linq datatable

我需要将C#中的两个DataTable与linq结合使用。

我卡在这里,Invalidcastexception

我不知道如何解决这个问题。

MySqlCommand cmdDataBaseKK = new MySqlCommand(" Select Date, Value as 'ValueK' from htdbmain.mainfsdb where field = 'KK' and ticker = '" + FinalTicker + "'", conn);
MySqlCommand cmdDataBaseJJ = new MySqlCommand(" Select Date, Value as 'ValueJ' from htdbmain.mainfsdb where field = 'JJ' and ticker = '" + FinalTicker + "'", conn);

DataTable DatasetJK = new DataTable();

try
{
    MySqlDataAdapter sdaK = new MySqlDataAdapter();
    sdaK.SelectCommand = cmdDataBaseKK;
    DataTable DatasetK = new DataTable();
    sdaK.Fill(DatasetK);

    MySqlDataAdapter sdaJ = new MySqlDataAdapter();
    sdaJ.SelectCommand = cmdDataBaseJJ;
    DataTable DatasetJ = new DataTable();
    sdaJ.Fill(DatasetJ);

    //DatasetK.Merge(DatasetJ);

    DataTable Table3 = new DataTable();
    Table3.Columns.Add("Date", typeof(String));
    Table3.Columns.Add("ValueK", typeof(Int32));
    Table3.Columns.Add("ValueJ", typeof(Int32));

    var Result = 
        from row1 in DatasetK.AsEnumerable()
        join row2 in DatasetJ.AsEnumerable()
            on row1.Field<String>("Date") equals row2.Field<String>("Date")
        select Table3.LoadDataRow(new object[]
        {
            row1.Field<String>("Date"),
            row1.Field<Int32>("ValueK"),
            row2.Field<Int32>("ValueJ"),
        }, false);

    Result.CopyToDataTable();

    BindingSource KSource = new BindingSource();
    KSource.DataSource = Table3;

    dataGridView1.DataSource = KSource;

我用简单条件尝试的相同数据表正在工作!

DataTable table1 = new DataTable();
table1.Columns.Add("Date", typeof(Int32));
table1.Columns.Add("ValueK", typeof(Int32));
table1.Rows.Add(1, 10);
table1.Rows.Add(2, 20);
table1.Rows.Add(3, 30);

DataTable table2 = new DataTable();
table2.Columns.Add("Date", typeof(Int32));
table2.Columns.Add("ValueJ", typeof(Int32));
table2.Rows.Add(1, 5);
table2.Rows.Add(2, 15);
table2.Rows.Add(3, 25);

DataTable table3 = new DataTable();
table3.Columns.Add("Date", typeof(Int32));
table3.Columns.Add("ValueK", typeof(Int32));
table3.Columns.Add("ValueJ", typeof(Int32));

var a = from row1 in table1.Rows.Cast<DataRow>()
    join row2 in table2.Rows.Cast<DataRow>()
        on row1["Date"] equals row2["Date"]
    select table3.LoadDataRow(new object[]
    {
        row1.Field<Int32>("Date"),
        row1.Field<Int32>("ValueK"),
        row2.Field<Int32>("ValueJ"),
    }, false);
a.CopyToDataTable();

BindingSource KSource = new BindingSource();
KSource.DataSource = table3;
dataGridView1.DataSource = KSource;

两个代码部分之间有什么区别?

1 个答案:

答案 0 :(得分:0)

我用这段代码解决了这个问题:

var Result = 
    from row1 in DatasetK.AsEnumerable()
    join row2 in DatasetJ.AsEnumerable()
        on row1["Date"] equals row2["Date"]
    select new
    {
        Date = row1["Date"],
        ValueK = row1["ValueK"],
        ValueJ = row2["ValueJ"]
    };

foreach (var rowInfo in Result)
{
    T3.Rows.Add(rowInfo.Date, rowInfo.ValueK, rowInfo.ValueJ);
}