本文介绍了追加值返回现有的dtolist的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我有以下代码,其中我将有一个foreach循环来返回我想要的数据。我有2个值,所以循环将发生两次。完成第一个循环后,数据将存储到DtoList中。随后,第二个循环将在第一个循环的数据下附加其数据。 但是我从下面的代码得到的内容将覆盖第一个循环中的现有数据并将其替换为来自第二个循环的数据。 如何在不丢失前一个循环的情况下将第二个循环中的数据附加到第一个循环中? 以下是我的代码: 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)) ; } } // 此处没问题 foreach (字符串 id in splittedId ) { dataModel = ManagerFactory.Instance.CustomerManager.GetCustomerByIdList( int .Parse(site),id); // 检索数据时没有错误 dataModel.Data1.DtoList.Add(dataModel.Data1 .DtoList); // 我被困在这里 } 我尝试过: 1.在线搜索一些解决方案,但没有avail。解决方案 你正在使用相同的列表来获取并再次向其中添加数据。 在循环中尝试这样,然后检查dataModel 。 foreach ( string id in splittedId) { var dataModel1 = ManagerFactory.Instance.CustomerManager.GetCustomerByIdList( int .Parse(site),id); dataModel.Data1.DtoList.Add(dataModel1.Data1.DtoList); } 或 foreach ( string id in splittedId) { dataModel.Data1.DtoList.Add(ManagerFactory.Instance.CustomerManager.GetCustomerByIdList( int .Parse(site),id).Data1.DtoList); } 请纠正我,如果我的话理解是错误的... 如果你说foreach循环运行两次,我假设splittedId List< string> object包含两个值不同的值。 在foreach循环中,您将通过id获取一个不同的客户,并为此客户分配dataModel对象,每次循环运行时都是不同的引用。你还没有从第一个循环运行中覆盖DtoList中的数据,你在每个循环运行中只有不同的dataModel实例,它包含自己的DtoList。 如果你想要要将数据附加到每个循环运行的公共列表,在循环外创建一个公共DtoList,并为循环内的每个dataModel附加到此。请参阅我创建的编码示例,模拟您的预期行为(您可以在编码区执行代码并查看打印出的附加值):编码地示例 [ ^ ] 问候, Mark。 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: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 hereforeach (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. 解决方案 you are using same list to fetch and again add data into it.try like this in loop then check the dataModel.foreach (string id in splittedId ){var dataModel1 = ManagerFactory.Instance.CustomerManager.GetCustomerByIdList(int.Parse(site), id);dataModel.Data1.DtoList.Add(dataModel1.Data1.DtoList);}orforeach (string id in splittedId ){dataModel.Data1.DtoList.Add(ManagerFactory.Instance.CustomerManager.GetCustomerByIdList(int.Parse(site), id).Data1.DtoList);}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. 这篇关于追加值返回现有的dtolist的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-21 23:57