Reusing an object causes List<T> redundancy
<p>I had a problem where I was getting data returned in a IQueryable object and it was repeated. I solved it by moving the temp object that was used to build the collection from method scoped to loop scoped. In other words it gets wiped out each time the loop repeats.</p>

<p>Original:</p>

<pre><code>private void MyMethod()
{
  SomeObj ProjDtl = new SomeObj(); //here is the obj method scoped


  foreach (ReservedSimProjects item in parameters.ReservedProjects.ReservedSimProj)
  {

    IQueryable&lt;SJ&gt; DbProj = SimColl.Where(e =&gt; e.SimProjectID == item.ProjectId);

     ProjDtl.ProjName = DbProj.First().ProjName;
     ProjDtl.ProjArea = DbProj.First().ProjArea;
     ProjDtl.ProjPerim = DbProj.First().ProjArea;
     ....etc....

     ProjectsDetails.Add(ProjDtl);
  }
</code></pre>

<p>Now to my obviously incorrect thinking ProjDtl would be created once and reused. Old values would be overwritten with new values as the loop iterates. And that seemed to be exactly what was happening. Stepping through the debugger the ProjDtl properties were being overwritten but then the last line where it was added to the collection the original from the first pass was added thus my collection had duplicates, triplicates etc. Changing the scope so the object get recreated every pass solved the problem. Why because there is some sort of pointer to the original object and it hasn't been updated even though every property on it was reassigned???</p>

<pre><code>private void MyMethod()
{

  foreach (ReservedSimProjects item in parameters.ReservedProjects.ReservedSimProj)
  {

     SomeObj ProjDtl = new SomeObj(); //here is the obj method scoped 

     IQueryable&lt;SJ&gt; DbProj = SimColl.Where(e =&gt; e.SimProjectID == item.ProjectId);

     ProjDtl.ProjName = DbProj.First().ProjName;
     ProjDtl.ProjArea = DbProj.First().ProjArea;
     ProjDtl.ProjPerim = DbProj.First().ProjArea;
     ....etc....

     ProjectsDetails.Add(ProjDtl);
  }
</code></pre>

<p>So for once I managed to solve my own problem but I'm not exactly sure why/what was solved.</p>

<p>JB</p>
