连接结果集

发布于 2024-10-10 05:12:25 字数 703 浏览 0 评论 0原文

我正在尝试为目录中的每个产品生成所有可能的产品选项/值组合的列表。每个产品可以有可变数量的选项,每个选项可以有可变数量的值。

例如,假设我有一件衬衫,选项/值是颜色(红色、黄色、蓝色)、尺寸(小号、米号、大号)和材质(棉、尼龙、混纺)。我想生成一个如下所示的列表:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

少女的英雄梦 2024-10-17 05:12:25

样板内容:

public class Color
{
    private readonly string _color;

    public Color(string color)
    {
        _color = color;
    }

    public override string ToString()
    {
        return _color;
    }
}

public class Size
{
    private readonly string _size;

    public Size(string size)
    {
        _size = size;
    }

    public override string ToString()
    {
        return _size;
    }
}

public class Material
{
    private readonly string _material;

    public Material(string material)
    {
        _material = material;
    }

    public override string ToString()
    {
        return _material;
    }
}

相关部分:

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            var colors = new List<Color>() { new Color("Red"), 
                                             new Color("Yellow"), 
                                             new Color("Blue") };

            var sizes = new List<Size>() { new Size("S"), 
                                           new Size("M"), 
                                           new Size("L") };

            var materials = new List<Material>() { new Material("Cotton"),
                                                   new Material("Nylon"),
                                                   new Material("Blend") };

            var products = from c in colors
                           from s in sizes
                           from m in materials
                           select new { Color = c, Size = s, Material = m };


            foreach (var p in products)
            {
                Console.WriteLine("{0}, {1}, {2}", p.Color, p.Size, p.Material);
            }
            Console.ReadKey(true);
        }
    }
}

The boiler plate stuff:

public class Color
{
    private readonly string _color;

    public Color(string color)
    {
        _color = color;
    }

    public override string ToString()
    {
        return _color;
    }
}

public class Size
{
    private readonly string _size;

    public Size(string size)
    {
        _size = size;
    }

    public override string ToString()
    {
        return _size;
    }
}

public class Material
{
    private readonly string _material;

    public Material(string material)
    {
        _material = material;
    }

    public override string ToString()
    {
        return _material;
    }
}

The relevant part:

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            var colors = new List<Color>() { new Color("Red"), 
                                             new Color("Yellow"), 
                                             new Color("Blue") };

            var sizes = new List<Size>() { new Size("S"), 
                                           new Size("M"), 
                                           new Size("L") };

            var materials = new List<Material>() { new Material("Cotton"),
                                                   new Material("Nylon"),
                                                   new Material("Blend") };

            var products = from c in colors
                           from s in sizes
                           from m in materials
                           select new { Color = c, Size = s, Material = m };


            foreach (var p in products)
            {
                Console.WriteLine("{0}, {1}, {2}", p.Color, p.Size, p.Material);
            }
            Console.ReadKey(true);
        }
    }
}
Smile简单爱 2024-10-17 05:12:25
var results =   from c in colours
                from s in sizes
                from m in materials
                select Tuple.Create(c, s, m);

或者您可以在最后一行创建一个匿名类型:

select new { Colour = c, Size = s, Material = m };
var results =   from c in colours
                from s in sizes
                from m in materials
                select Tuple.Create(c, s, m);

or you could create an anonymous type in the last line:

select new { Colour = c, Size = s, Material = m };
盗心人 2024-10-17 05:12:25

它可以很简单:

public class Product
{
  public string Colour { get; set; }
  public string Size { get; set; }
  public string Material { get; set; }
}

IList<Product> products = new List<Product>();
foreach (string colour in colours)
{
  foreach (string size in sizes)
  {
    foreach (string material in materials)
    {
      products.Add(new Product 
      {
        Colour = colour,
        Size = size,
        Material = material
      });
    }
  }
}

其中 colourssizesmaterials 是值的字符串数组。这是你所期待的吗?

It could be as simple as:

public class Product
{
  public string Colour { get; set; }
  public string Size { get; set; }
  public string Material { get; set; }
}

IList<Product> products = new List<Product>();
foreach (string colour in colours)
{
  foreach (string size in sizes)
  {
    foreach (string material in materials)
    {
      products.Add(new Product 
      {
        Colour = colour,
        Size = size,
        Material = material
      });
    }
  }
}

Where colours, sizes and materials are string arrays of the values. Is this what you were expecting?

你爱我像她 2024-10-17 05:12:25

这实际上是交叉连接,而不是串联。

您可以在 C# 中使用 LINQ 或嵌套 foreach 循环非常简单地完成此操作。 LINQ 方式非常赏心悦目。假设颜色、尺寸和面料都已作为集合存在于您的代码中。

from color in colors
from size in sizes
from fabric in fabrics
select new {
    color,
    size,
    fabric
}

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.

from color in colors
from size in sizes
from fabric in fabrics
select new {
    color,
    size,
    fabric
}
山有枢 2024-10-17 05:12:25

还要考虑:

public enum Color    {Red, Yellow, Blue};
public enum Size     {S, M, L};
public enum Material {Cotton, Nylon, Blend};
public class Product
{
   public Color    color;
   public Size     size;
   public Material material;
}

List<Product> products = new List<Product>();

int numColors    = Enum.GetValues(typeof(Color)).Length;
int numSizes     = Enum.GetValues(typeof(Size)).Length;
int numMaterials = Enum.GetValues(typeof(Material)).Length;

for(int i = 0; i < numColors; i++)
   for(int j = 0; k < numSizes; j++)
       for(int k = 0; k < numMaterials; k++)
       {
          Product p  = new Product();
          p.color    = (Color)i;
          p.size     = (Size)j;
          p.material = (Material)k;
          products.Add(p);
       }

Also consider:

public enum Color    {Red, Yellow, Blue};
public enum Size     {S, M, L};
public enum Material {Cotton, Nylon, Blend};
public class Product
{
   public Color    color;
   public Size     size;
   public Material material;
}

List<Product> products = new List<Product>();

int numColors    = Enum.GetValues(typeof(Color)).Length;
int numSizes     = Enum.GetValues(typeof(Size)).Length;
int numMaterials = Enum.GetValues(typeof(Material)).Length;

for(int i = 0; i < numColors; i++)
   for(int j = 0; k < numSizes; j++)
       for(int k = 0; k < numMaterials; k++)
       {
          Product p  = new Product();
          p.color    = (Color)i;
          p.size     = (Size)j;
          p.material = (Material)k;
          products.Add(p);
       }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文