C# 需要一个递归函数来解决典型的电子商务多变量问题
假设我有以下数据(为了可读性,使用伪代码): 我可以通过LINQ获得数据,如下所示:C# 需要一个递归函数来解决典型的电子商务多变量问题,c#,recursion,e-commerce,C#,Recursion,E Commerce,假设我有以下数据(为了可读性,使用伪代码): 我可以通过LINQ获得数据,如下所示: var myVariations = myProduct.Variations.ToList(); 如何将这些变化映射到这样的结构中(对于易趣交易API): var ebayVariations = [ { Name = "Red-S-34", Value = [ // yes, these
var myVariations = myProduct.Variations.ToList();
如何将这些变化映射到这样的结构中(对于易趣交易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
数组只包含一个值这一事实有点奇怪;但对于eBay的交易API,如果我在一个变量中列出多个值(与这种递归的东西相比,这很容易做到),它会抱怨或者,如果您熟悉易趣交易API,我如何才能让它以“最佳”的方式工作,与易趣预期的变体上市方式保持一致(如果您愿意,可以通过AddFixedPricedItem调用)。我对易趣交易API一无所知,但是(最后一步是放弃递归,转而使用聚合)。我对术语做了一些无关紧要的更改,但写了一些澄清的评论
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,
};
}
}
我不知道如何将我的变体模型与CartesianProduct方法相适应。你解决了吗?我已经为你开始了一个实现,然后被召集到一个会议上。很抱歉,但我希望它能起作用。好吧,我解决了它。我得到了字符串结果,如:“Color=White,Size=s,Length=36”和“Color=White,Size=S,Length=38”等…我不知道如何维护对象结构。因此,在将对象传递给CartesianProduct函数之前,我将名称和值连接到字符串中;基本上:
variations.Select(v=>v.Values.Select(a=>v.Name+“=”+a.Value))
(我的实际实现为这些值提供了一些额外的元数据,这就是为什么它们是对象,而不是数组的原因)。我仍在努力,很可能会接受这个答案。谢谢和+1!+1。我已经实现了Linq/CartesianProduct方法的一个黑客版本。我现在在这个问题上花费了太多时间,但我稍后会回到你的答案,看看它是否更合适。@alex sashok想知道你是否可以帮助解决这个问题N
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();