0

Hi i'm trying to setup simple test data.

I simply want to take a collection which is smallish and make it bigger by add itself multiple times.

After I;ve added them together i want to re-number the property LineNumber so that there are no duplicates and that it goes in order. 1,2,3,4....

no matter what i try it doesn't seem to work and i cant see the mistake.

var sampleTemplateLine = dataContext.TemplateFileLines.ToList();


*//tired this doesnt work either*           
//List<TemplateFileLine> lineRange = new List<TemplateFileLine>();
//lineRange.AddRange(sampleTemplateLine);
//lineRange.AddRange(sampleTemplateLine);
//lineRange.AddRange(sampleTemplateLine);
//lineRange.AddRange(sampleTemplateLine);

var allProducts = sampleTemplateLine
                       .Concat(sampleTemplateLine)
                       .Concat(sampleTemplateLine)
                       .Concat(sampleTemplateLine)
                       .ToList();
int i = 1;
foreach (var item in allProducts)
{
     item.LineNumber = i;
     i++;
}

this doesnt seem to work either

  //re-number the line number
  var total = allProducts.Count();
  for (int i =0; i < total; i++)
  {
      allProducts[i].LineNumber = i+1;
  }

PROBLEM: below RETURN 4 when i'm expecting 1

var itemthing = allProducts.Where(x => x.LineNumber == 17312).ToList();
Seabizkit
  • 2,417
  • 2
  • 15
  • 32
  • 4
    In short - the problem is when you add the same list 4 times, you have 4 references to the same object in the new list. When you edit one of them - you edit all (4) of them. – Bojan B Jun 21 '17 at 06:36

2 Answers2

4

You are adding the same objects multiple times. You wold have to add new objects or clone the ones you have.

Pablo notPicasso
  • 3,031
  • 3
  • 17
  • 22
0

The problem is they are pointing the same object. So if you change a property it changes all the pointed objects at the same

You can use Clone method if it exist, if not you can create your own Clone method like in this question.

sertsedat
  • 3,490
  • 1
  • 25
  • 45