Click here to Skip to main content
15,895,142 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, I have the following codes in which I will have a foreach loop to return the data I want. I have 2 values so the loop will happen twice. After first loop is done, data will be stored into a DtoList. Subsequently, second loop will append its data under the data from first loop.
But what I get from the codes below will overwrite the existing data in first loop and replace it with data from second loop.
How can I achieve in appending the data from second loop into first loop without losing the former one?
Below are my codes:
C#
DataModel<NewDtoList> dataModel = new DataModel<NewDtoList>();
var splittedId = new List<string>();

for (int i = 0; i < oldId.Length; i += 4)
{
    if (oldId.Length - i >= 4)
    {
        splittedId .Add(oldId.Substring(i, 4));
    }
    else
    {
        splittedId .Add(oldId.Substring(i, oldId.Length - i));
    }
} // no problem until here

foreach (string id in splittedId )
{
    dataModel = ManagerFactory.Instance.CustomerManager.GetCustomerByIdList(int.Parse(site), id); // no error in retrieving data
    dataModel.Data1.DtoList.Add(dataModel.Data1.DtoList); // I am getting stuck here
}


What I have tried:

1. Search online for some solution but no avail.
Posted
Updated 29-Nov-16 3:01am

you are using same list to fetch and again add data into it.
try like this in loop then check the dataModel.
C#
foreach (string id in splittedId )
{
var dataModel1 = ManagerFactory.Instance.CustomerManager.GetCustomerByIdList(int.Parse(site), id);
dataModel.Data1.DtoList.Add(dataModel1.Data1.DtoList);
}
or
foreach (string id in splittedId )
{
dataModel.Data1.DtoList.Add(ManagerFactory.Instance.CustomerManager.GetCustomerByIdList(int.Parse(site), id).Data1.DtoList);

}
 
Share this answer
 
Comments
Jamie888 29-Nov-16 2:02am    
Sir, I have tried both of your methods but unfortunately, they did not work.
Jawad Ahmed Tanoli 29-Nov-16 2:13am    
you debug it ? same problem your facing it is not appending new data in dataModel right?
Jamie888 29-Nov-16 2:14am    
It has red error line below the codes that you suggested.
Jawad Ahmed Tanoli 29-Nov-16 2:15am    
you try this one?
foreach (string id in splittedId )
{
var dataModel1 = ManagerFactory.Instance.CustomerManager.GetCustomerByIdList(int.Parse(site), id);
dataModel.Data1.DtoList.Add(dataModel1.Data1.DtoList);
}
can you read the error what is that?
Jawad Ahmed Tanoli 29-Nov-16 2:20am    
this is your all code any other loop there??
Hi,

Please correct me if my understanding is wrong...

If you say that the foreach loop is run twice, I am assuming that the splittedId List<string> object contains 2 values which are distinct in value.

Inside the foreach loop, you are getting a distinct customer by id, and the dataModel object you assign this customer to, is a different reference each time the loop runs. You have not overwritten the data in DtoList from the first loop run, you just have different instances of dataModel in each loop run which contains its own DtoList.

If you want to append the data to a common list for each loop run, create a common DtoList outside the loop and append to this for each dataModel within the loop. Please see this coding ground example I created, simulating your expected behaviour (You can execute the code in the coding ground and see the appended values printed out): Coding Ground Example[^]

Regards,
Mark.
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900