zoukankan      html  css  js  c++  java
  • List泛型Add后,所有值都是最后一次Add的结果 解决方法

    public List<VoucherEntryEntity> AnalyseVoucherEntry(DataTable voucherentry)
            {

                VoucherEntryEntity voucherentryentity = new VoucherEntryEntity();
                List<VoucherEntryEntity> listVoucherEntity = new List<VoucherEntryEntity>();
                for (int i = 0; i < voucherentry.Rows.Count;i++ )
                {
                    voucherentryentity.FBrNo = voucherentry.Rows[i]["FBrNo"].ToString();
                    voucherentryentity.FExplanation = voucherentry.Rows[i]["FExplanation"].ToString();
                    voucherentryentity.FAccountID = Convert.ToInt32(voucherentry.Rows[i]["FAccountID"]);
                    voucherentryentity.FDetailID = Convert.ToInt32(voucherentry.Rows[i]["FDetailID"]);
                    voucherentryentity.FCurrencyID = Convert.ToInt32(voucherentry.Rows[i]["FCurrencyID"]);
                    voucherentryentity.FExchangeRate = float.Parse(voucherentry.Rows[i]["FExchangeRate"].ToString());
                    voucherentryentity.FDC = Convert.ToInt32(voucherentry.Rows[i]["FDC"]);
                    voucherentryentity.FAmountFor = Convert.ToDecimal(voucherentry.Rows[i]["FAmountFor"]);
                    voucherentryentity.FAmount = Convert.ToDecimal(voucherentry.Rows[i]["FAmount"]);
                    voucherentryentity.FQuantity = float.Parse(voucherentry.Rows[i]["FQuantity"].ToString());
                    voucherentryentity.FMeasureUnitID = Convert.ToInt32(voucherentry.Rows[i]["FMeasureUnitID"]);
                    voucherentryentity.FUnitPrice = float.Parse(voucherentry.Rows[i]["FUnitPrice"].ToString());
                    voucherentryentity.FInternalInd = voucherentry.Rows[i]["FInternalInd"].ToString();
                    voucherentryentity.FAccountID2 = Convert.ToInt32(voucherentry.Rows[i]["FAccountID2"]);
                    voucherentryentity.FSettleTypeID = Convert.ToInt32(voucherentry.Rows[i]["FSettleTypeID"]);
                    voucherentryentity.FSettleNo = voucherentry.Rows[i]["FSettleNo"].ToString();
                    voucherentryentity.FTransNo = voucherentry.Rows[i]["FTransNo"].ToString();
                    voucherentryentity.FCashFlowItem = Convert.ToInt32(voucherentry.Rows[i]["FCashFlowItem"]);
                    voucherentryentity.FTaskID = Convert.ToInt32(voucherentry.Rows[i]["FTaskID"]);
                    voucherentryentity.FResourceID = Convert.ToInt32(voucherentry.Rows[i]["FResourceID"]);
                    listVoucherEntity.Add(voucherentryentity);
                }
                return listVoucherEntity;
            }

    添加多条数据时,所有的值都是最后一次的结果。

    原来在.net中,list 在ADD时,每次保留一个它的引用,所以要把VoucherEntryEntity voucherentryentity = new VoucherEntryEntity();
    放到for循环中去才可以避免ADD后的值不会重复,也就是每循环一次,VoucherEntryEntity 就要new 一次.

     

     

    public List<VoucherEntryEntity> AnalyseVoucherEntry(DataTable voucherentry)
            {

                
                List<VoucherEntryEntity> listVoucherEntity = new List<VoucherEntryEntity>();
                for (int i = 0; i < voucherentry.Rows.Count;i++ )
                {

                    VoucherEntryEntity voucherentryentity = new VoucherEntryEntity();
                    voucherentryentity.FBrNo = voucherentry.Rows[i]["FBrNo"].ToString();
                    voucherentryentity.FExplanation = voucherentry.Rows[i]["FExplanation"].ToString();
                    voucherentryentity.FAccountID = Convert.ToInt32(voucherentry.Rows[i]["FAccountID"]);
                    voucherentryentity.FDetailID = Convert.ToInt32(voucherentry.Rows[i]["FDetailID"]);
                    voucherentryentity.FCurrencyID = Convert.ToInt32(voucherentry.Rows[i]["FCurrencyID"]);
                    voucherentryentity.FExchangeRate = float.Parse(voucherentry.Rows[i]["FExchangeRate"].ToString());
                    voucherentryentity.FDC = Convert.ToInt32(voucherentry.Rows[i]["FDC"]);
                    voucherentryentity.FAmountFor = Convert.ToDecimal(voucherentry.Rows[i]["FAmountFor"]);
                    voucherentryentity.FAmount = Convert.ToDecimal(voucherentry.Rows[i]["FAmount"]);
                    voucherentryentity.FQuantity = float.Parse(voucherentry.Rows[i]["FQuantity"].ToString());
                    voucherentryentity.FMeasureUnitID = Convert.ToInt32(voucherentry.Rows[i]["FMeasureUnitID"]);
                    voucherentryentity.FUnitPrice = float.Parse(voucherentry.Rows[i]["FUnitPrice"].ToString());
                    voucherentryentity.FInternalInd = voucherentry.Rows[i]["FInternalInd"].ToString();
                    voucherentryentity.FAccountID2 = Convert.ToInt32(voucherentry.Rows[i]["FAccountID2"]);
                    voucherentryentity.FSettleTypeID = Convert.ToInt32(voucherentry.Rows[i]["FSettleTypeID"]);
                    voucherentryentity.FSettleNo = voucherentry.Rows[i]["FSettleNo"].ToString();
                    voucherentryentity.FTransNo = voucherentry.Rows[i]["FTransNo"].ToString();
                    voucherentryentity.FCashFlowItem = Convert.ToInt32(voucherentry.Rows[i]["FCashFlowItem"]);
                    voucherentryentity.FTaskID = Convert.ToInt32(voucherentry.Rows[i]["FTaskID"]);
                    voucherentryentity.FResourceID = Convert.ToInt32(voucherentry.Rows[i]["FResourceID"]);
                    listVoucherEntity.Add(voucherentryentity);
                }
                return listVoucherEntity;
            }

  • 相关阅读:
    atitit.解决SyntaxError: missing ] after element list"不个object 挡成个str eval ....
    atitit.软件开发概念过滤和投影 数据操作
    atitit.词法分析的实现token attilax总结
    Atitit.软件gui按钮and面板通讯子系统(区) github 的使用....
    Atitit.解决org.hibernate.DuplicateMappingException: Duplicate class/entity mapping
    atitit.故障排除 当前命令发生了严重错误。应放弃任何可能产生的结果sql server 2008
    Atitit.注解解析(1)词法分析 attilax总结 java .net
    atitit.软件开发GUI 布局管理优缺点总结java swing wpf web html c++ qt php asp.net winform
    atitit.报表最佳实践oae 与报表引擎选型
    Atitit. 解决unterminated string literal 缺失引号
  • 原文地址:https://www.cnblogs.com/htys/p/3168848.html
Copyright © 2011-2022 走看看