连接结果集

时间:2011-01-02 22:21:25

标签: c# combinations

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

所以,例如说我有一件衬衫,选项/值是颜色(红色,黄色,蓝色),尺寸(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#,但任何类型的代码示例都非常有用。非常感谢任何建议!

5 个答案:

答案 0 :(得分:3)

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 };

答案 1 :(得分:3)

锅炉板材:

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);
        }
    }
}

答案 2 :(得分:2)

可能很简单:

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是值的字符串数组。这是你期待的吗?

答案 3 :(得分:0)

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

您可以使用LINQ或嵌套的foreach循环在C#中完成此操作。 LINQ的方式非常简单。假设颜色,大小和结构都已作为代码存在于代码中。

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

答案 4 :(得分:0)

还要考虑:

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);
       }