开发者

get possible combinations in DataRow for given value

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

开发者_如何学Go

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


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


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 ?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜