需要一个递归函数来解决典型的电子商务多变量问题

时间:2011-07-06 18:07:05

标签: c# recursion e-commerce ebay

假设我有以下数据(伪代码中的可读性):

var myVariations = [
    { Name = "Color", Values = ["Red", "Yellow", "Green" /*, etc. */] },
    { Name = "Size", Values = ["S", "M", "L" /*, etc. */] },
    { Name = "Length", Values = ["34", "35", "36" /*, etc. */] },
    /* and so on...(up to 5 total) */
];

我可以像LINQ那样获取数据:

var myVariations = myProduct.Variations.ToList();

如何将这些变体映射到这样的结构中(对于eBay Trading API):

var ebayVariations = [
    {
        Name = "Red-S-34",
        Value = [
                                       // yes, these are arrays with only one item
            { Name = "Color", Values = [{Value = "Red"}] },
            { Name = "Size", Values = [{Value = "S"}] },
            { Name = "Length", Values = [{Value = "34" }] }
        ]
    },
    /* etc for all possible combinations */
];

显然,Values数组只保存一个值的事实有点奇怪;但是如果我在单个Variation中列出多个值(与这个递归的东西相比很容易做到),那么使用eBay的Trading API就会抱怨。 另外,如果您熟悉eBay Trading API,我怎样才能以“最佳”方式使其工作,符合eBay预期的变体列出方式(通过AddFixedPricedItem调用) ,如果你关心)。

2 个答案:

答案 0 :(得分:1)

我对eBay Trading API一无所知,但是here's an article on computing a Cartesian Product with LINQ(最后一步是递归而不是聚合)。

答案 1 :(得分:1)

我的术语变得微不足道,但写了澄清的评论。

    public IEnumerable<Combination> GetCombinations(Variation[] variations, int variationIndex, IEnumerable<VariationPosition> aggregatedPositions)
    {
        // We should choose one position from every variation, 
        // so we couldn't produce combination till we reach end of array. 
        if (variationIndex < variations.Length)
        {
            // Pick current variation.  
            var currentVariation = variations[variationIndex];

            // Every variation has list of possible positions (Color could be Green, Redm, Blue, etc.).
            // So we should walk through all the positions 
            foreach (var val in currentVariation.Positions)
            {
                // Current position. Variation's name will be used during creating result Combination.
                var position = new VariationPosition()
                {
                    Name = currentVariation.Name,
                    Value = val
                };
                // Add position to already aggregated on upper levels of recursion positions.
                var newPositions = aggregatedPositions.Concat(Enumerable.Repeat(position, 1));

                // So we picked some variation position 
                // Let's go deeper.
                var combinations = this.GetCombinations(variations, variationIndex + 1, newPositions );

                // This piece of code allows us return combinations in iterator fashion.
                foreach (var combination in combinations)
                {
                    yield return combination;
                }
            }
        }
        else
        {
            // We reached end of variations array 
            // I mean we have one position of every variation. 

            // We concatenate name of positions in order to create string like "Red-S-34"
            var name = aggregatedPositions.Aggregate("", (res, v) => res += v.Name);

            // This code is a little bit naive, I'm too lazy to create proper infrastructure,
            // But its mission is to create content for property Value of your ebayVariations item. 
            var value = aggregatedPositions
                .Select(v => new { Name = v.Name, Values = new[] { new { Value = v.Value } } })
                .ToArray();

            // And we return completed combination.
            yield return new Combination()
            {
                Name = name,
                Value = value,
            };
        }
    }

用法:

var allCombinations = this.GetCombinations(inputVariations, 0, new VariationPosition[0]).ToArray();