vb.net - Combine the second element of Array -
i have array of objects called orders holding list of item guids , quantity.
dim orders object(,) = {{"itema", 11}, {"itemb", 10}, {"itema", 2}, {"itemb", 1}}
i want convert following:
dim orders object(,) = {{"itema", 13}, {"itemb", 11}}
in other words, want have distinct select of items combined quantity of records item. can linq or should use loop?
you can use dictionary(of tkey, tvalue)
group guid
dim orders object(,) = {{"itema", 11}, {"itemb", 10}, {"itema", 2}, {"itemb", 1}} dim orderssum new dictionary(of string, int32)() orderindex int32 = 0 orders.getupperbound(0) if orderssum.containskey(orders(orderindex, 0)) = true orderssum(orders(orderindex, 0)) += orders(orderindex, 1) else orderssum.add(orders(orderindex, 0), orders(orderindex, 1)) end if next
then if dictionary
type not allowed , need multidimensional array return type
dim result object(,) = new object(orderssum.count - 1, 1) {} dim index int32 = 0 each key string in orderssum.keys result(index, 0) = key result(index, 1) = orderssum(key) index += 1 next
Comments
Post a Comment