Question Comparing two data objects and make a third with the results...

MarshallRichard

New member
Joined
May 13, 2013
Messages
2
Programming Experience
10+
What i am trying to do is to compair two data objects (_itemSupplier & _item) and depending on if the id's match, write data from one of the two data objects into a third data object (_results) to use for a datasource. I'm drawing a complete blank on how to get this accomplish.

My two data models are like the following;
VB.NET:
Public Class ItemSupplier
 Public Property ItemId as Integer
 Public Property ItemName as String
 Public Property SupplierId as Integer
 Public Property SupplierName as String
End Class

Public Class Item
 Public Property ItemId as Integer
  Public Property itemName as String
End Class

My variables are like the following;
VB.NET:
Dim _itemSupplier as list(of ItemSupplier) = <fill list with data>
Dim _item as list(of Item) = <fill list with data>
Dim _results as list(of itemSupplier) = nothing

The variables are filled with data like;
VB.NET:
_itemSupplier.index(0) contains = 1,"paper",1,"office depot"

_item.index(0) contains 1,"paper"
_item.index(1) contains 2,"pen"

Some pseudo-code that i wrote up;
VB.NET:
For Each i in Item
If _itemSupplier.ItemId = _item.ItemId then
  
  _results.itemId = _itemSupplier.itemId
  _results.itemName = _itemSuppler.itemName 
  _results.SupplierId = _itemSupplier.SupplierId
  _results.SupplierName = _itemSupplier.SupplierName
Else
  _results.itemId = _item.ItemId
  _results.itemName = _item.ItemName
  _results.SupplierId = Nothing
  _results.SupplierName = nothing
  
End if
Next

deserved output would looks something like this;

Item Name | Supplier Name
--------------------------------------
paper | <nothing>
pen | office depot
 
To be clear, you want to copy from '_itemSupplier' to '_results' if there is a matching ItemID in '_item', correct? Do you really need to create a copy of the ItemSupplier object or could you just put the existing instance in the other list as well?

By the way, '_item' and '_itemSuppiler' are bad variable names. Those variables refer to List objects so their names should reflect that, i.e. they should be plural, i.e. use '_items' and '_itemSuppliers'. I would consider Item to be a bad type name too, because it is so generic. It seems like you're dealing with retail stock so I'd use StockItem instead. The term "item" is used very often to refer to anything in a list so it can get confusing.
 
_results needs to contain all records in the _item list, with the corresponding SupplierId & SupplierName from the _itemSupplier list if there is a match. If not, then SupplierId & SupplierName is set to Nothing.

When I tried to do a nested For Each loop, it appeared to work correctly until I ran into a record that had both items (paper & pen) in the _itemSupplier list. When this happen, I ended up with the items listed twice with two set correctly and the other two set to nothing.

The variable names and data I used in my example are only that, for the example but thanks for the input.
 

Latest posts

Back
Top