martolu
martolu

Reputation: 31

Update first Datatable depends on second Datatable with multiple key Condition

I have a situation to upadate TABLEA depends on TABLEB but the comaparision key in more than one. With Single comparision key is no problem

Can anyone have some sugession regarding this?

Here i used below code.

                        _dtMaster.AsEnumerable().Join(_dtChild.AsEnumerable(),
                         _dtmater => Convert.ToString(_dtmater[ss.TrimStart().TrimEnd()]),
                         _dtchild => Convert.ToString(_dtchild[ss.TrimStart().TrimEnd()]),
                         (_dtmater, _dtchild) => new { _dtmater, _dtchild }).ToList().ForEach(o =>
                         {
                             for (int i = 0; i <= Field.Count() - 1; i++) //Set value to table
                                 o._dtmater.SetField(Field[i], o._dtchild[Field[i]].ToString());
                         });

Where _dtmaster in a datatable where i want to update data from _dtchild table This example is a single key comparison. How can i make this for multiple keys?

Thank you

Upvotes: 0

Views: 15

Answers (1)

Tim Schmelter
Tim Schmelter

Reputation: 460138

You can use an anonymous type to Join:

var joined = from m in _dtMaster.AsEnumerable()
             join c in _dtChild.AsEnumerable()
                 on new { Col1 = m.Field<string>("Col1"), Col2 = m.Field<string>("Col2") }
             equals new { Col1 = c.Field<string>("Col1"), Col2 = c.Field<string>("Col2") }
             select new { MasterRow = m, ChildRow = c };

foreach (var both in joined)
{ 
    foreach (DataColumn mCol in both.MasterRow.Table.Columns)
    {
        both.MasterRow.SetField(mCol, both.ChildRow[mCol.Ordinal]);
    }
}

Upvotes: 1

Related Questions