C# 如何合并两个数据表并合并到新的数据表中

C# 如何合并两个数据表并合并到新的数据表中,c#,.net,asp.net-mvc,linq,datatable,C#,.net,Asp.net Mvc,Linq,Datatable,我有一个代码,可以读取工作簿中的所有excel文件,并将其保存到数据集中的datatables中。现在,基于某些条件,我必须将数据表合并到新的数据表中,并存储到oracle数据库中。以下代码用于将两个数据表dt1和dt2合并到新表dtnew中,但结果很奇怪。如果第一个datatable有3行,第二个datatable有6行,则会给出18行结果,而不是只给出匹配的行值,这意味着这里有3行同时包含两个表值。简而言之,我创建了第三个表dtnew,并设计了结构,然后使用条件。逻辑上有什么错误或者有更好的

我有一个代码,可以读取工作簿中的所有excel文件,并将其保存到数据集中的datatables中。现在,基于某些条件,我必须将数据表合并到新的数据表中,并存储到oracle数据库中。以下代码用于将两个数据表dt1和dt2合并到新表dtnew中,但结果很奇怪。如果第一个datatable有3行,第二个datatable有6行,则会给出18行结果,而不是只给出匹配的行值,这意味着这里有3行同时包含两个表值。简而言之,我创建了第三个表
dtnew
,并设计了结构,然后使用条件。逻辑上有什么错误或者有更好的方法吗

DataSet ds = Exceltotable(@name);
                        for (int i = 0; i < ds.Tables.Count; i++)
                        {
                            if (ds.Tables[i].TableName == "Table2")
                            {
                                dt1 = ds.Tables[i];
                            }
                            if (ds.Tables[i].TableName == "Table3")
                            {
                                dt2 = ds.Tables[i];
                            }
                            if (dt1 != null && dt2 != null)
                            {
                                break;
                            }
                        }
                        System.Data.DataTable dtnew = new System.Data.DataTable();
                        dtnew.Columns.Add("LOCATION", typeof(string));
                        dtnew.Columns.Add("RBSSHARE", typeof(string));
                        dtnew.Columns.Add("VENDOR", typeof(string));
                        dtnew.Columns.Add("PWRTYPE", typeof(string));
                        dtnew.Columns.Add("EQPTINSTYPE", typeof(string));
                        dtnew.Columns.Add("BTSHEIGHT", typeof(string));
                        dtnew.Columns.Add("BTSFAN", typeof(string));
                        dtnew.Columns.Add("HMSAVAIL", typeof(string));
                        dtnew.Columns.Add("MODELNAME", typeof(string));
                        dtnew.Columns.Add("MODELNO", typeof(string));
                        dtnew.Columns.Add("SERIALNO", typeof(string));
                        dtnew.Columns.Add("REVNO", typeof(string));
                        dtnew.Columns.Add("TECHNOLOGY", typeof(string));
                        dtnew.Columns.Add("Asset description", typeof(string));
                        dtnew.Columns.Add("Asset Category", typeof(string));
                        dtnew.Columns.Add("ASSETTAG", typeof(string));
                        dtnew.Columns.Add("ADDREMARK", typeof(string));
                        dtnew.Columns.Add("DELIVERYDATE", typeof(string));

                        var query = from  pp in dt1.AsEnumerable() 
                                    join ii in dt2.AsEnumerable()
                                        on pp.Field<string>("LOCATION") equals
                                            ii.Field<string>("LOCATION")

                                    select dtnew.LoadDataRow(new object[]
{
pp.Field<string>("LOCATION"),
pp.Field<string>("RBSSHARE"),
pp.Field<string>("VENDOR"),
pp.Field<string>("PWRTYPE"),
pp.Field<string>("EQPTINSTYPE"),
pp.Field<string>("BTSHEIGHT"),
pp.Field<string>("BTSFAN"),
pp.Field<string>("HMSAVAIL"),
pp.Field<string>("MODELNAME"),
pp.Field<string>("MODELNO"),
pp.Field<string>("SERIALNO"),
pp.Field<string>("REVNO"),
ii.Field<string>("TECHNOLOGY"),
ii.Field<string>("TECHNOLOGY"),
ii.Field<string>("Asset description"),
ii.Field<string>("ASSETTAG"),
ii.Field<string>("ADDREMARK"),
ii.Field<string>("DELIVERYDATE")
}, false);

                        query.CopyToDataTable();
                        ds.Tables.Add(dtnew);
DataSet ds=Exceltotable(@name);
对于(int i=0;i
尝试以下代码:

var query = 
    from row1 in dt1.AsEnumerable()
    from row2 in dt2.AsEnumerable()
                   .Where(x => x["LOCATION"] == row1["LOCATION"])
                   .DefaultIfEmpty()
    select dtNew.LoadDataRow(new object[]
    {
        row1.Field<string>("LOCATION"),
        row1.Field<string>("RBSSHARE"),
        row1.Field<string>("VENDOR"),
        row1.Field<string>("PWRTYPE"),
        row1.Field<string>("EQPTINSTYPE"),
        row1.Field<string>("BTSHEIGHT"),
        row1.Field<string>("BTSFAN"),
        row1.Field<string>("HMSAVAIL"),
        row1.Field<string>("MODELNAME"),
        row1.Field<string>("MODELNO"),
        row1.Field<string>("SERIALNO"),
        row1.Field<string>("REVNO"),
        row2 == null ? null : row2.Field<string>("TECHNOLOGY"),
        row2 == null ? null : row2.Field<string>("TECHNOLOGY"),
        row2 == null ? null : row2.Field<string>("Asset description"),
        row2 == null ? null : row2.Field<string>("ASSETTAG"),
        row2 == null ? null : row2.Field<string>("ADDREMARK"),
        row2 == null ? null : row2.Field<string>("DELIVERYDATE")
    }, false);
var查询=
来自dt1.AsEnumerable()中的行1
来自dt2.AsEnumerable()中的第2行
。其中(x=>x[“位置”]==row1[“位置”])
.DefaultIfEmpty()
选择dtNew.LoadDataRow(新对象[])
{
第1行。字段(“位置”),
第1行字段(“RBSSHARE”),
第1行字段(“供应商”),
第1行。字段(“PWRTYPE”),
第1行字段(“eqptinsype”),
第1行字段(“BTSHEIGHT”),
第1行字段(“BTSFAN”),
第1行字段(“HMSAVAIL”),
第1行。字段(“模型名”),
第1行字段(“型号”),
第1行。字段(“序列号”),
第1行字段(“修订号”),
row2==null?null:row2.Field(“技术”),
row2==null?null:row2.Field(“技术”),
row2==null?null:row2.Field(“资产描述”),
row2==null?null:row2.Field(“ASSETTAG”),
row2==null?null:row2.Field(“ADDREMARK”),
row2==null?null:row2.Field(“DELIVERYDATE”)
},假);

我不知道出了什么问题,看起来逻辑是正确的,但真的不知道出了什么问题。它再次生成了cartisian Product,给出了18行-这是因为字段位置不是唯一的。您应该按唯一字段联接表。DefaultIfEmpty()-它是左连接,应该可以工作。数据是从复制到datatable的excel文件生成的。因此,如果我们可以忘记位置的唯一性,我们是否有其他机制从左表中提取所有值并从第二个表中提取匹配值如果第一个表中的许多元素与第二个表中的许多元素相关联,那么您应该添加第三个表TableOneInTableTwo,其结构为{id,tableOneId,tableTwoId}我添加了这样的结构,只有na?只有当
LOCATION
在每个数据表中都是唯一的时,它才会像预期的那样工作。