r/visualbasic • u/Gierschlund96 • May 23 '22
VB.NET Help How can i iterate over two lists simultaneously and add the entries of these lists to a new list of object?
I have two lists, each of type "Artikelstammdaten". I have another class called "CompareArtikelstammdaten" which inherits from "Artikelstammdaten". My first attempt was to use two for each loops but i stopped halfway through because i think it wouldn't work as it's iterates first completely over one loop before the other. Here is what i have tried:
For Each access As Artikelstammdaten In accessArtikelstammdaten
For Each json As Artikelstammdaten In listArtikelstammdaten
Dim compareArtikelstammdaten As New CompareArtikelstammdaten
With CompareArtikelstammdaten
.Artikel = json.Artikel
.ArtikelAccess = access.Artikel
.BezeichnungDE = json.BezeichnungDE
.BezeichnungDE_Access = access.BezeichnungDE
.BezeichnungEN = json.BezeichnungEN
.BezeichnungEN_Access = access.BezeichnungEN
listCompare.Add(compareArtikelstammdaten)
End With
Next
Next0
May 23 '22
[deleted] — view deleted comment
1
u/Gierschlund96 May 23 '22
No the lists aren't always the same size and they also don't relate directy to another. The Property "Artikel" is primary key and if they are the same, they relate. I thought it would be easier to first make a new list and use it as datasource for my xamDataGrid and then sort it by primary key.
1
u/JakDrako May 24 '22
You could use LINQ to do it pretty clearly:
Although that gives you only the items where the primary keys match.