Popular List c #

0

I am trying to return data from a list in a foreach. In the first foreach, it is returning the correct data, but it is not writing to the nflist variable of the first foreach, so as soon as it passes through it again, it loses the previous data and replaces it with the current data.

    public void UpdateNFE()
    {
        List<NFE> nfeList = new List<NFE>();
        NFEDB nfeData = new NFEDB();
        SAPBAPI sapBAPI = new SAPBAPI();
        NFE nfeTeste = new NFE();

        DataTable NFSap =  sapBAPI.SearchNFbyDate();

        foreach (DataRow item in NFSap.Rows)
        {
            int index = NFSap.Rows.IndexOf(item);

            nfeList = nfeData.ListNFEForUpdate(NFSap.Rows[index]["J_1BNFDOC-NFENUM"].ToString(), NFSap.Rows[index]["J_1BNFDOC-CREDAT"].ToString());

    //está retornando todos os dados corretamente, mas é necessário "gravar" 
    //os dados retornados

        }

        foreach (NFE nfe in nfeList)
        {
            StructureNFEAccessKey nfeAccessKey = new StructureNFEAccessKey();

            nfeAccessKey.REGIO = nfe.Region;
    }
    
asked by anonymous 16.10.2017 / 01:22

1 answer

2

You need to use the nfeList.Add (item) or nfeList.AddRange (items) method to add items to your list, otherwise the old values will be lost even.

Replace:

  nfeList = nfeData.ListNFEForUpdate(NFSap.Rows[index]["J_1BNFDOC-NFENUM"].ToString(), NFSap.Rows[index]["J_1BNFDOC-CREDAT"].ToString());

If nfeData.ListNFEForUpdate is a list, insert this:

nfeList.AddRange(nfeData.ListNFEForUpdate(NFSap.Rows[index]["J_1BNFDOC-NFENUM"].ToString(), NFSap.Rows[index]["J_1BNFDOC-CREDAT"].ToString()));

If nfeData.ListNFEForUpdate is a single object of type NFE, enter this:

nfeList.Add(nfeData.ListNFEForUpdate(NFSap.Rows[index]["J_1BNFDOC-NFENUM"].ToString(), NFSap.Rows[index]["J_1BNFDOC-CREDAT"].ToString()));
    
16.10.2017 / 01:40