简体   繁体   中英

How to match data from two data tables have single row using LINQ or C# other way

I have two data-table like below getting data from other datatables

DataTable dt  
DataTable dt2 

Datatable dt have below value and every time dt and dt2 have single row values only then easy match them

F1    F2   F3    F4
Yes   No   Yes   No

Data-table dt2 have below value

F1    F2   F3
Yes   Yes  Yes

I want to match output like below into Data-table dt3, both on column name

F1   F3
Yes  Yes 

See datatable dt and datable dt2 both have only one row every time, so you can match dynamic because both have one row only with column name and value, so I not am getting how to match with dynamic using LINQ or some other way for better understanding please check the below image

在此处输入图片说明

I believe someone might able to do better but here is mine. ※ Simplified the answer a bit.

private static DataTable RegenerateDataTableInCommon(DataTable dt, DataTable dt2)
{
    var dtRows = dt.Rows.Cast<DataRow>();
    var dt2Rows = dt2.Rows.Cast<DataRow>();
    var dtColumnNames = dt.Columns.Cast<DataColumn>().Select(dtC => dtC.ColumnName);

    var columnNameMatched = dt2.Columns.Cast<DataColumn>().Select(dtC => dtC.ColumnName).Where(colName => dtColumnNames.Contains(colName));
    var cellValueMatched = columnNameMatched.Where(colName => dtRows.First()[colName] == dt2Rows.First()[colName]);

    DataTable outDt = new DataTable();
    outDt.Clear();
    DataRow nRow = outDt.NewRow();
    foreach (var colName in cellValueMatched)
    {
        outDt.Columns.Add(colName);
        nRow[colName] = dtRows.First()[colName];
    }
    outDt.Rows.Add(nRow);

    return outDt;
}

If without linq is an option then you can try the below method:

public DataTable getMatchedColumnAndValue(DataTable dt1, DataTable dt2)
{
   try
   {

      var ndt = new DataTable();
      //creating columns for the table
      var dt1columns = dt1.Columns.Cast<DataColumn>().Select(s => s.ColumnName).ToList();
      var dt2columns = dt2.Columns.Cast<DataColumn>().Select(s => s.ColumnName).ToList();
      var MatchedCol = dt1columns.Intersect(dt2columns).ToList();
      foreach (var col in MatchedCol)
      {
         ndt.Columns.Add(col);
      }
      //creating columsn matcehd row
      var drnew = new string[MatchedCol.Count];
      for (int i = 0; i < MatchedCol.Count; i++)
      {
         if (dt1.Rows[0][MatchedCol[i]].ToString() == dt2.Rows[0][MatchedCol[i]].ToString())
              drnew[i] = dt1.Rows[0][MatchedCol[i]].ToString();
         else
              drnew[i] = null;
      }
      ndt.Rows.Add(drnew);

      //removing null value columns
      foreach (var col in MatchedCol)
      {
         if (ndt.AsEnumerable().All(dr => dr.IsNull(col)))
             ndt.Columns.Remove(col);
      }

      return ndt;
    }
    catch (Exception ex)
    {
       throw ex;
    }
}

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