Home > Net >  Updating DataTable multiple columns from another joined DataTable through same value
Updating DataTable multiple columns from another joined DataTable through same value

Time:07-07

I know a code like,

datatable_A.AsEnumerable().Join(datatable_B.AsEnumerable(),                                                                                                                                     
dt2Rows => dt2Rows.Field<string>("name"),                                                                                                                                         
dt1Rows => dt1Rows.Field<string>("name"),
(dt2Rows, dt1Rows) => new { dt2Rows, dt1Rows })
.ToList()
.ForEach(i => i.dt2Rows.SetField("phone number", i.dt1Rows.Field<string>("phone number")));

And now, I need to do multiple lambda expression like,

.ForEach(i => i.dt2Rows.SetField("phone number", i.dt1Rows.Field<string>("phone number")),
         m => m.dt2Rows.SetField("address", m.dt1Rows.Field<string>("address")),
         k => k.dt2Rows.SetField("nickname", k.dt1Rows.Field<string>("nickname")),
         d => d.dt2Rows.SetField("body weight", d.dt1Rows.Field<string>("body weight"))
         );

How can I do like this ? The answer should not be lambda expression and any kind of answers are ok.

Thank you !

CodePudding user response:

if I understand your problem right, you want to do multiple operations in every iteration of ForEach, and the i, m, k, d are the same value in every run, is that right?

in that case, you can simply do this:

.ForEach(i => {
    i.dt2Rows.SetField("phone number", i.dt1Rows.Field<string>("phone number"));
    i.dt2Rows.SetField("address", m.dt1Rows.Field<string>("address"));
    i.dt2Rows.SetField("nickname", k.dt1Rows.Field<string>("nickname"));
    i.dt2Rows.SetField("body weight", d.dt1Rows.Field<string>("body weight"));
});
  • Related