0

in .Net is there any way to find matching combinations for given value in data table ?

data table is like

ItemID, Name, Quantity

1, A, 2

2, B, 1

3, C, 3

4, D, 4

5, E, 5

need to get possible combinations with matching Quantity column.

E.G. if I passed 8

need result of DataTable (2+1+5 = 8, 3+5 = 8, 1,3,4 = 8) as

combinationID, Quantity, NoOfItems(Row Count for combinationID)

1, 2, 3

1, 1, 3

1, 5, 3

2, 3, 2

2, 5, 2

3, 1, 3

3, 3, 3

3, 4, 3

2 Answers2

0

You could use a recursive CTE to find all combination like in this SO-Question: Getting all possible combinations which obey certain condition with MS SQL

Community
  • 1
  • 1
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
0

I got idea from C# algorithm - find least number of objects necessary

but still in trouble to modify with my requirement. Coz. in my case there is duplicate values,

var list = new[] { 2, 3, 4, 6, 7, 2, 4, 5, 3 };
Array.Sort<int>(list);
var ans = Knapsack(list, 9);

it will trow a exception InvalidOperationException ("Sequence contains no elements")

any idea to resolve this ?

Community
  • 1
  • 1
  • got this http://stackoverflow.com/questions/83547/algorithm-to-find-which-numbers-from-a-list-of-size-n-sum-to-another-number –  Jan 21 '11 at 03:43