简体   繁体   中英

Adding data to a property of type List<>

I am extracting data from an excel sheet and placing that into class properties. In the below shown piece of code I am trying to add data into a property of type List<> . It executes without errors but the List<> count is 1 all the time. as shown in the below example the for loop runs 5 times but the rah.rahTermDate count is 1 after existing the for loop.

for (int i = 0; i < 5; i++)
  {                  
    RawACAHireTermDates rahd = new RawACAHireTermDates();
    rahd.RawHireDate = Convert.ToDateTime(GetCellValue(GetCell(sheetData, Cells[i, k], j), workbookPart, false, true));
    rahd.RawTermDate = Convert.ToDateTime(GetCellValue(GetCell(sheetData, Cells[i, (k + 1)], j), workbookPart, false, true));
    rah.rahTermDate = new List<RawACAHireTermDates> { rahd };
    //  rah.rahTermDate.Add(rahd);               
  }

Business object :

public class MasterClientGroupSheetData
    {
    public class RawACAHireTermDates
    {     
        public DateTime? RawHireDate { get; set; }
        public DateTime? RawTermDate { get; set; }                   
    }

    public class RawACAHireTermEmployee
    {
 ...........
        public List<RawACAHireTermDates> rahTermDate { get; set; }
    }
}

This code is re-initializing your List on each iteration and losing the previous data.

rah.rahTermDate = new List<RawACAHireTermDates> { rahd };

You should define your list outside of the for loop, and then add items to it using .add()

You are recreating your list in the loop. Create it outside the loop once:

rah.rahTermDate = new List<RawACAHireTermDates>();

for (int i = 0; i < 5; i++)
{                  
    RawACAHireTermDates rahd = new RawACAHireTermDates();
    rahd.RawHireDate = Convert.ToDateTime(GetCellValue(GetCell(sheetData, Cells[i, k], j), workbookPart, false, true));
    rahd.RawTermDate = Convert.ToDateTime(GetCellValue(GetCell(sheetData, Cells[i, (k + 1)], j), workbookPart, false, true));
    rah.rahTermDate.Add(rahd);
    // rah.rahTermDate = new List<RawACAHireTermDates> { rahd }; <-- this line was the problem
}

You create new list every iteration rah.rahTermDate = new List<RawACAHireTermDates> { rahd }; which contains only one item, which was processed the same iteration. To fix this problem define list outside for loop, in loop add elements to the list and assign rah.rahTermDate after for loop.

This line is the culprit:

rah.rahTermDate = new List<RawACAHireTermDates> { rahd };

Every time the loop runs, you are setting a NEW list to rah.rahTermDate .

Your code should be more like this:

//before the loop, declare a new List
rah.rahTermDate = new List<RawACAHireTermDates>();

for (int i = 0; i < 5; i++)
  {                  
    RawACAHireTermDates rahd = new RawACAHireTermDates();
    rahd.RawHireDate = Convert.ToDateTime(GetCellValue(GetCell(sheetData, Cells[i, k], j), workbookPart, false, true));
    rahd.RawTermDate = Convert.ToDateTime(GetCellValue(GetCell(sheetData, Cells[i, (k + 1)], j), workbookPart, false, true));

    //add to the existing list...
    rah.rahTermDate.Add(rahd);               
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM