I'm trying to write a function where it will have two list:
- original list (fooList)
- list that contains extra info (fooWithExtList)
But not sure why when I concat the text in another list, it also update the information in my original list.
Here's the code:
var fooDataList = new List<Foo>();
fooDataList.Add(new Foo { Bar = "Test1" });
fooDataList.Add(new Foo { Bar = "Test2" });
fooDataList.Add(new Foo { Bar = "Test3" });
fooDataList.Add(new Foo { Bar = "Test4" });
var fooList = new List<Foo>();
var fooWithExtList = new List<Foo>();
//assign foodata to fooList
fooDataList.ForEach(fdl => fooList.Add(fdl));
//assign foodata to fooWithExtList
fooDataList.ForEach(fdl => fooWithExtList.Add(fdl));
//set the fooWithExtList with extra info
fooWithExtList.ForEach(fwel => fwel.Bar = fwel.Bar + "ext");
//merge the list
fooList = fooList.Concat(fooWithExtList).ToList();
Result:
Test1ext Test2ext Test3ext Test4ext Test1ext Test2ext Test3ext Test4ext
Expecting:
Test1 Test2 Test3 Test4 Test1ext Test2ext Test3ext Test4ext
dot net fiddle here: https://dotnetfiddle.net/0nMTmX