猿问

如何在没有列名的情况下使用 linq 彻底连接两个数据表

这可能在其他地方得到了回答,但我还没有找到可行的解决方案。


我有两个数据表,我想将它们连接到一个数据表中,其中包含来自它们两个的所有数据,或者至少来自它们中的第一个和第二个数据表中的一些列。


我不想列出第一个数据表中的所有列(总共 180 个)。我试过例如。这个


var JoinedResult = from t1 in table1.Rows.Cast<DataRow>()

                   join t2 in table2.Rows.Cast<DataRow>() 

                      on Convert.ToInt32(t1.Field<string>("ProductID")) equals t2.Field<int>("FuelId")

                    select t1;

但这仅给出了 table1 中的列。如何从 table2 中获取列到我的结果?最后,我需要将我的结果添加到数据集中。


ResultSet.Tables.Add(JoinedResult.CopyToDataTable());

编辑:


我最终以此作为解决方案。这遵循此处给出的示例Create join with Select All (select *) in linq to datasets


 DataTable dtProduct = dsProduct.Tables[0];

 DataTable dtMoistureLimits = ds.Tables[0];


 //clone dt1, copies all the columns to newTable 

 DataTable dtProductWithMoistureLimits = dtProduct.Clone();


 //copies all the columns from dt2 to newTable 

foreach (DataColumn c in dtMoistureLimits.Columns)

   dtProductWithMoistureLimits.Columns.Add(c.ColumnName, c.DataType);


   var ProductsJoinedWithMoistureLimits = dtProduct.Rows.Cast<DataRow>()

       .Join(dtMoistureLimits.Rows.Cast<DataRow>(),// join table1 and table2

       t1 => new { ProductID = t1.Field<int>("ProductID"), DelivererID = t1.Field<int>("DelivererID") },

       t2 => new { ProductID = t2.Field<int>("MoistureLimits_ProductID"), DelivererID = t2.Field<int>("MoistureLimits_DelivererID") },

       (t1, t2) =>     // when they match 

       {    // make a new object

            // containing the matching t1 and t2

           DataRow row = dtProductWithMoistureLimits.NewRow();

           row.ItemArray = t1.ItemArray.Concat(t2.ItemArray).ToArray();

           dtProductWithMoistureLimits.Rows.Add(row);

           return row;

       });

但是,在dtMoistureLimits 中,dtProduct中没有所有“ProductID”和“DelivererID”的行。目前我的解决方案只返回匹配的行。


如何改进解决方案以返回那些在dtMoistureLimits 中没有“ProductID”和“DelivererID”数据的行?


婷婷同学_
浏览 185回答 2
2回答

拉风的咖菲猫

使用方法语法的解决方案,无需提及所有列var result = table1.Rows.Cast<DataRow>()&nbsp; &nbsp;.Join(table2.Rows.Cast<DataRow>(),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // join table1 and table2&nbsp; &nbsp; &nbsp; t1 => Convert.ToInt32(t1.Field<string>("ProductID")) // from every t1 get the productId&nbsp; &nbsp; &nbsp; t2 => t2.Field<int>("FuelId")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // from every t2 get the fuelId,&nbsp; &nbsp; &nbsp; (t1, t2) => new&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // when they match&nbsp;&nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // make a new object&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;T1 = t1,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // containing the matching t1 and t2&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;T2 = t2,&nbsp; &nbsp; &nbsp; }
随时随地看视频慕课网APP
我要回答