0

I'm fairly new to C# so please bear with me...

I have a dataset with 2 tables:

tables

What I want to do is update the ID on the Cost table where Premises[PremNo] equals Cost[PremNo].

So far I have a foreach that goes through all the rows and updates them, I just don't know how to specify where I need it to update from

        foreach (DataRow row in ds.Tables[1].Rows)
        {
            foreach (DataColumn ID in ds.Tables[1].Columns)
            {
                row["ID"] = **12345**;
            }

        }

I need someone to point me in the direction for the 12345 bit.

ds.Tables[1] is Cost, and ds.Tables[0] is Premises

Any help would be greatly appreciated.

Dan

  • 1
    Possible duplicate of [Inner join of DataTables in C#](http://stackoverflow.com/questions/665754/inner-join-of-datatables-in-c-sharp) – CDove Mar 07 '17 at 16:17

1 Answers1

0

try this..

 var tblCost = ds.Tables[1];
 var tblPremises = ds.Tables[0];
 foreach (DataRow cost in tblCost.Rows)
 {
    var premRow = tblPremises.AsEnumerable().Where(row => row.Field<int>("PremNo") == cost.Field<int>("PremNo")).FirstOrDefault();
    if (premRow != null)
        cost["ID"] = premRow.Field<int>("ID");
 }
levent
  • 3,464
  • 1
  • 12
  • 22