I have a list of items that contains the above properties. Id Amount PackageNbr What I need to accomplish, is to get from this list, the items that have the NEAREST OR EQUAL sum of Amount to a specific value; but a condition should always be valid which is that the items returned should be from different PackageNbr. For example.
listOfItems:
╔══════╦══════════╦═════════════╗
║ ID ║ amount ║ packageNbr ║
╠══════╬══════════╬═════════════╣
║ 1 ║ 10 ║ 1 ║
║ 2 ║ 7 ║ 1 ║
║ 3 ║ 4 ║ 1 ║
║ 4 ║ 6 ║ 2 ║
║ 5 ║ 5 ║ 2 ║
║ 6 ║ 3 ║ 2 ║
║ 7 ║ 10 ║ 3 ║
║ 8 ║ 9 ║ 3 ║
║ 9 ║ 3 ║ 4 ║
║ 10 ║ 2 ║ 5 ║
╚══════╩══════════╩═════════════╝
for the value 21, id of returnedItems are : 1 - 6 - 8
for the value 14, id of returnedItems are : 3 - 7
for the value 11, id of returnedItems are : 4 - 9 - 10
I think the above can be achieve by getting all the sum combination (that have different PackageNbr), and after that we can get the NEAREST OR EQUAL.
Any idea how to accomplish this task ? I did surf the web and didn't find a way to do it using linq. Any help would be appreciated.
Regards