连接结果集
我正在尝试为目录中的每个产品生成所有可能的产品选项/值组合的列表。每个产品可以有可变数量的选项,每个选项可以有可变数量的值。
例如,假设我有一件衬衫,选项/值是颜色(红色、黄色、蓝色)、尺寸(小号、米号、大号)和材质(棉、尼龙、混纺)。我想生成一个如下所示的列表:
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# 工作,但任何类型的代码示例都会非常有帮助。非常感谢您的任何建议!
I am trying to generate a list of all possible product option/value combinations for each product in a catalog. Each product can have a variable number of options, and each option can have a variable number of values.
So, for example say I had a shirt, and the option/values were color (red, yellow, blue), size (s, m, l), and material (cotton, nylon, blend). I want to generate a list that looks like this:
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
I know that in theory, this could generate a LOT of results, but in practice most of the products only have two or three options with two or three values each.
I'm working in C#, but any kind of code example would be extremely helpful. Thanks very much for any suggestions!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
样板内容:
相关部分:
The boiler plate stuff:
The relevant part:
或者您可以在最后一行创建一个匿名类型:
or you could create an anonymous type in the last line:
它可以很简单:
其中
colours
、sizes
和materials
是值的字符串数组。这是你所期待的吗?It could be as simple as:
Where
colours
,sizes
andmaterials
are string arrays of the values. Is this what you were expecting?这实际上是交叉连接,而不是串联。
您可以在 C# 中使用 LINQ 或嵌套 foreach 循环非常简单地完成此操作。 LINQ 方式非常赏心悦目。假设颜色、尺寸和面料都已作为集合存在于您的代码中。
This is actually a cross-join, not concatenation.
You could do this pretty simply in C# using LINQ or nested foreach loops. The LINQ way is pretty easy on the eyes. Assuming colors, sizes, and fabrics all exist as collections in your code already.
还要考虑:
Also consider: