1

I'm exporting some data from database to excel using CLOSEDXML. I need to add the corresponding objects/item to each object in the 'newModelist' IQueryable and add all the object to new List. In my case new row were added in the list using list.add method, however the old record is replaced by the next row/object. I'm not quite what would be the correct way to implement this.

var getData = _db.Database.Where(y => y.identifier == Identifier);
var newModelist = new MainModel();
var listXML= new List<listXML>();

 foreach(var item in getData) 
    {
        newModelist.col1 = item.A;
        newModelist.col2 = item.B;
        newModelist.col2 = item.C;
        listXML.Add(newModelist);
    }
Veck
  • 125
  • 1
  • 3
  • 13
  • 1
    Move this line of code inside the `foreach` loop: `var newModelist = new MainModel();` – Markus Meyer Jul 12 '22 at 11:33
  • Thank You @Markus Meyer, I wasn't aware that the new instance was off my foreach loop. Anyway Appreciate this :) – Veck Jul 13 '22 at 12:18

2 Answers2

2

You need to understand what an instance is.

You are currently modifying the same object instance. And you are adding the same instance to the List on every iteration.

Since List only stores the reference to an instance, your List currently contains multiple references to the same instance. Each index will return the same element.

If you want individual object instances you must create each using the new keyword:

foreach(var item in getData) 
{
    var newModelist = new MainModel
    {
        col1 = item.A,
        col2 = item.B,
        col3 = item.C
    };

    listXML.Add(newModelist);
}
BionicCode
  • 1
  • 4
  • 28
  • 44
  • Thank you mate @BionicCode. I didn't realized I have declared a new instance . my Bad XD. Appreciate your help! :) – Veck Jul 13 '22 at 12:16
  • 1
    This happens. That's why we have code reviews in our professional business ;) I'm happy I could help. – BionicCode Jul 13 '22 at 12:45
1

MainModel is a reference type and modifying the value will reflect in the all the references, so you need to create a new instance while setting the properties in the loop. Moving creation of newModelList to for loop would do

foreach(var item in getData) 
    {
        var newModelist = new MainModel();
        newModelist.col1 = item.A;
        newModelist.col2 = item.B;
        newModelist.col2 = item.C;
        listXML.Add(newModelist);
    }
Viresh Mathad
  • 576
  • 1
  • 5
  • 19
  • It's not that the list is a reference type, it's that the items inside the list are reference types that cause this problem. – DavidG Jul 12 '22 at 11:43
  • List is a reference type, true. But this is not the reason for the observed behavior. The reason is that he is modifying the *same* instance. And because this instance is a reference type, the List effectively contains a single object (instance) or multiple references that all point to the same instance. – BionicCode Jul 12 '22 at 11:51
  • I meant model which has "list" keyword. Lemme edit. Thanks – Viresh Mathad Jul 12 '22 at 11:52
  • newModelList is a variable, not a type. MainModel is a type. The variable newModelList is of type MainModel. MainModel is a reference type. – BionicCode Jul 12 '22 at 11:56
  • right, my bad. Hope this time everything is correct – Viresh Mathad Jul 12 '22 at 11:59
  • Looks much better now. Thanks for fixing it. – BionicCode Jul 12 '22 at 12:02