简体   繁体   中英

Linq join table and create dynamic column error

I would like to do something create dynamic column in Linq for my joined table.

This is my code after asking around. But it give me another error like this.

Code:

var SelectedDT1AndDt2= from dr1 in dt1.AsEnumerable()
                       join dr2 in dt2.AsEnumerable()
                       on dr1.Field<Int64>("id1") equals 
                       dr2.Field<Int64>("id2") into joinDt1AndDt2
                       from leftjoin in joinDt1AndDt2.DefaultIfEmpty()
                       select dtJoinedTable.LoadDataRow(
                       (from dc1 in dt1.Columns.Cast<DataColumn>() 
                        select dc1.ColumnName.ToString()).ToArray()
                        .Union(from dc2 in dt2.Columns.Cast<DataColumn>()
                        select dc2.ColumnName).ToArray()
                       , false);
SelectedDT1AndDt2.CopyToDataTable();

Error:

Input string was not in a correct format.Couldn't store in id Column. Expected type is Int64.

这是因为列导入的类型不是 Int'64',我再次检查并确认原始 col 类型是 Int'32'。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM