public static void CreatePurchaseOrders(string empId, List <long> itemIds, List <long> itemsFirstSupplierIds, List <int> quantities) { //Key: SupplierId //Value: List of Tuple of Item Id and Corresponding Quantity Dictionary <long, List <Tuple <long, int> > > uniquePurchaseOrders = new Dictionary <long, List <Tuple <long, int> > >(); string newOrderNumber; //To group items with same alt supplier together into one Purchase Order for (int i = 0; i < itemIds.Count; i++) { if (quantities[i] > 0) { if (uniquePurchaseOrders.ContainsKey(itemsFirstSupplierIds[i])) { uniquePurchaseOrders[itemsFirstSupplierIds[i]].Add(new Tuple <long, int>(itemIds[i], quantities[i])); } else { List <Tuple <long, int> > selectedItemsIdsAndQuantites = new List <Tuple <long, int> >(); uniquePurchaseOrders.Add(itemsFirstSupplierIds[i], selectedItemsIdsAndQuantites); uniquePurchaseOrders[itemsFirstSupplierIds[i]].Add(new Tuple <long, int>(itemIds[i], quantities[i])); } } } foreach (KeyValuePair <long, List <Tuple <long, int> > > po in uniquePurchaseOrders) { //Create new record under PurchaseOrder table. newOrderNumber = PurchaseOrderDAO.GetNewOrderNumber(); StockDAO.CreatePurchaseOrder(po.Key, po.Value, newOrderNumber, long.Parse(empId)); } }