Popular List c#

Asked

Viewed 213 times

0

I’m trying to return data from a list in a foreach. In the first foreach, it is returning the correct data, but it is not recording in the nflist variable of the first foreach, so as soon as it passes through it again, it loses the previous data and overwrites by 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;
    }
  • If you do nfeList = ... within the foreach naturally loses the previous values. If the idea is to add things to the list you will have to use the method Add of the list for that

  • Thanks! Worked out with nfeList.Addrange..

1 answer

2


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

substitute:

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

If nfeData.Listnfeforupdate is a list, enter 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 NFE object, enter this:

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.