0

在此处输入图像描述

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声明

4

1 回答 1

0

只需使用 linq 将两者结合起来。请记住包含using System.Linq;在您的 using 子句中。

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();
于 2013-07-31T07:08:57.220 回答