public class Item
{
public int ID {get;set;}
public string SKU {get;set;
public int Quantity {get;set;}
}
I pull items from my database like:
List<Items> savedItems = Db.GetItems();
I then have this method that takes in a collection of items:
public void Update(List<Items> items)
{
// update database
}
The items passed into the Update method items that may or may not be already in the database. Uniqueness has to be done using the SKU property.
So say I have in the database:
ID SKU qty
1 a 1
2 b 1
3 c 1
Now if I pass a items collection into the Update method, that has:
ID=?, SKU=d, qty=1
Then it will just insert a new item.
If I pass in a item that looks like:
ID=?, SKU=b, qty=1
then I won't insert a new item, but I will update the item with ID=2.
How can I do this? Should I just put all the SKU values into a hash, if it exists, then somehow find that item and update it?
I will I could find the Item w/o doing a database lookup since I already pulled the items from the database already.
If the item is already in the database, I have to update the quantity. If it is not in the db, it just gets inserted.