If I do a dummy setup and test on your code:
public class MachineAD
{
public string Name;
public int ID;
}
public class Machine
{
public string BarCode;
}
public class MachineUnified
{
public int IDad;
public string NameFromAD;
public string NameFromInventory;
}
private void myButton_Click(object sender, EventArgs e)
{
List<MachineAD> machinesAD = new List<MachineAD>();
machinesAD.Add(new MachineAD() { Name = "M1", ID = 33333 });
machinesAD.Add(new MachineAD() { Name = "M2", ID = 33435 });
machinesAD.Add(new MachineAD() { Name = "M3", ID = 32673 });
List<Machine> machines = new List<Machine>();
machines.Add(new Machine() { BarCode = "1" });
machines.Add(new Machine() { BarCode = "3" });
List<MachineUnified> machinesUnified = new List<MachineUnified>();
int i = 0;
foreach (MachineAD ADmachine in machinesAD)
{
foreach (Machine machine in machines)
{
if (ADmachine.Name.Contains(machine.BarCode))
{
machinesUnified.Add(new MachineUnified() { IDad = ADmachine.ID, NameFromAD = ADmachine.Name, NameFromInventory = machine.BarCode });
Console.WriteLine(machinesUnified[i].NameFromAD);
i++;
}
}
}
}
Then what I get is two rows:
33333, M1, 1
32673, M3, 3
Which is what I would expect. To get more rows than that, I need to change the Barcode so it is a subset of several machine names:
List<MachineAD> machinesAD = new List<MachineAD>();
machinesAD.Add(new MachineAD() { Name = "M1", ID = 33333 });
machinesAD.Add(new MachineAD() { Name = "M2", ID = 33435 });
machinesAD.Add(new MachineAD() { Name = "M3", ID = 32673 });
List<Machine> machines = new List<Machine>();
machines.Add(new Machine() { BarCode = "M" });
machines.Add(new Machine() { BarCode = "3" });
Then I get 4 rows:
33333, M1, M
33435, M2, M
32673, M3, M
32673, M3, 3
So...I'd start by looking at your data and specifically what is in the BarCode field - if any of them are "odd" then they could match quite a few of your names instead of just one, which would cause your problem.