Click here to Skip to main content
15,886,774 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm having trouble structuring my code to do what I want.

The goal is to purge an inventory database and replace it with only the computers that have been in use the last year

I am comparing 2 lists of computers, one from Active Directory and one from an inventory database. I've made the tables into List<>'s, and my idea was that the "valid" list of computers in Active Directory, would be checked item by item against the entire inventory list, then if there was a match, information from both lists would be consolidated into a third list. I am not getting that result

My code
C#
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++;
        }
    }
}

Output excerpt, (it's 1700 lines compared with 4500, resulting in this pattern):
...033333
033333
033333
033333
033333
033333
033333
033333
033333
033333
033333
033333
033333
033435
033435
033435
033435
033435
033435
033435
033435
033435
033435
033435
033435
033435
032673
032673
032673
032673
032673
032673
032673
032673
032673
032673
032673
032673
032673
034567
034567
034567
034567
034567
034567
034567
034567
034567
034567
034567
034567
034567...


What I have tried:

Reading documentation, forum posts, articles on list cross-referencing etc. etc. etc.
Posted
Updated 14-Jul-16 8:16am
v2
Comments
Maciej Los 14-Jul-16 13:31pm    
Seems, you want to compare two deifferent objects. So, you have to implement Equals method to be able to compare those objects.
Frank R. Haugen 14-Jul-16 13:53pm    
I need the x.contains()
Maciej Los 14-Jul-16 15:19pm    
No, you don't. All what you need is to define method to compare objects. See OriginalGriff's answer.
Frank R. Haugen 14-Jul-16 15:40pm    
Yes I have, and he didn't modify my code

1 solution

If I do a dummy setup and test on your code:
C#
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:
C#
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.
 
Share this answer
 
Comments
Maciej Los 14-Jul-16 15:17pm    
5! and another one for effort!
Frank R. Haugen 14-Jul-16 15:39pm    
Wow, thank you for spending that much effort on this!

It really helped to know that the code was basically sound. After seeing how your code worked, I did some "writelines" and I missed a fundamental thing:
Breaking out of the innermost loop when the if was true. Very noob mistake
OriginalGriff 14-Jul-16 15:56pm    
"Very noob mistake" - and one we all make even now... :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900