【发布时间】:2011-01-02 22:21:24
【问题描述】:
我正在尝试为目录中的每个产品生成所有可能的产品选项/值组合的列表。每个产品可以有不定数量的选项,每个选项可以有不定数量的值。
例如,假设我有一件衬衫,选项/值是颜色(红色、黄色、蓝色)、尺寸(s、m、l)和材料(棉、尼龙、混纺)。我想生成一个如下所示的列表:
red, s, cotton
red, s, nylon
red, s, blend
red, m, cotton
red, m, nylon
red, m, blend
red, l, cotton
red, l, nylon
red, l, blend
yellow, s, cotton
yellow, s, nylon
yellow, s, blend
yellow, m, cotton
yellow, m, nylon
yellow, m, blend
yellow, l, cotton
yellow, l, nylon
yellow, l, blend
blue, s, cotton
blue, s, nylon
blue, s, blend
blue, m, cotton
blue, m, nylon
blue, m, blend
blue, l, cotton
blue, l, nylon
blue, l, blend
我知道理论上这会产生很多结果,但实际上大多数产品只有两个或三个选项,每个选项有两个或三个值。
我正在使用 C#,但任何类型的代码示例都会非常有帮助。非常感谢您的任何建议!
【问题讨论】:
-
对于其中一些答案,您使用的 .Net 版本很重要。使用 Tuple 的需要 4.0,任何使用 LINQ 的都需要 3.5+。
标签: c# combinations