使用Foreach语句在C#中进行多个数组插入

时间:2013-07-31 05:44:49

标签: c# arrays foreach-loop-container

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()); }
        }

有没有其他正确的方法来使用这种代码来避免重复?我知道它是重复的,因为有两个foreach语句

1 个答案:

答案 0 :(得分:0)

使用linq将两者结合起来。请记住在您的使用条款中加入using System.Linq;

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);
}

或者,您可以使用查询表达式执行此操作:

var result = (from l1 in ArrayFunc.QueryResult
             from l2 in ArrayFunc.QueryResult1       
             select new DRFGAModifiedRecord { RecordDocID = l1.FGACol1 ... }).ToList();