Multiple Array Insertion in C# using Foreach statement -


enter image description here

private void updated_modrecfga()         {             try             {                 drfgamodifiedrecord table = new drfgamodifiedrecord();                 foreach (arrayfunc row in arrayfunc.queryresult)                 {                     foreach (arrayfunc row2 in arrayfunc.queryresult1)                     {                         table.recorddocid = row.fgacol1;                         table.drno = row.fgacol2;                         table.ddnum = row.fgacol3;                         table.linenumber = row.fgacol4;                         table.itemnmbr = row2.upditemcode;                         table.itemdesc = row2.upditemdesc;                         table.pallet = row2.updpallets;                         table.bagsno = row2.updbagsno;                         table.totalloaded = row2.updtotalkgs;                         table.poststat = row2.updpoststat;                         table.prodcode = row2.updprodcode;                         table.variantcode = row2.updvariantcode;                         table.datemodify = datetime.now.toshortdatestring();                         table.timemodify = datetime.now.toshorttimestring();                         table.usermodify = "mik";//globalvarclass.loguser;                         table.reasonmodify = globalvarclass.getmodreasondr;                         table.filetype = "new";                          saverec(table);                         gfunc.msgbox("saved", 1);                     }                 }                  arrayfunc.queryresult.clear();                 arrayfunc.queryresult1.clear();                 globalvarclass.getmodreasondr = string.empty;             }             catch (exception ex) { messagebox.show(ex.message.tostring()); }         } 

is there proper way use kind of code avoid duplicate? know duplicate because of 2 foreach statement

just combine 2 using linq. remember include using system.linq; in using clause.

var results = arrayfunc.queryresult.selectmany(l1 => arrayfunc.queryresult1, (l1, l2) =>    new drfgamodifiedrecord   {         recorddocid = l1.fgacol1,         drno = l1.fgacol2,         ddnum = l1.fgacol3,         linenumber = l1.fgacol4,         itemnmbr = l2.upditemcode,         itemdesc = l2.upditemdesc,         pallet = l2.updpallets,         bagsno = l2.updbagsno,         totalloaded = l2.updtotalkgs,         poststat = l2.updpoststat,         prodcode = l2.updprodcode,         variantcode = l2.updvariantcode,         datemodify = datetime.now.toshortdatestring(),         timemodify = datetime.now.toshorttimestring(),         usermodify = "mik",//globalvarclass.loguser         reasonmodify = globalvarclass.getmodreasondr,         filetype = "new"   }).tolist();  foreach (row in results) {   saverec(row);   gfunc.msgbox("saved", 1); } 

alternatively can query expression:

var result = (from l1 in arrayfunc.queryresult              l2 in arrayfunc.queryresult1                     select new drfgamodifiedrecord { recorddocid = l1.fgacol1 ... }).tolist(); 

Comments

Popular posts from this blog

basic authentication with http post params android -

vb.net - Virtual Keyboard commands -

css - Firefox for ubuntu renders wrong colors -